php mail_runner从文件夹而不是数据库字段发送附件

php mail_runner sending attachment from folder instead of database field

提问人:JKG79 提问时间:3/8/2023 最后编辑:JKG79 更新时间:3/8/2023 访问量:33

问:

我正在尝试编写一个 php 脚本,该脚本使用 php mail_runner发送附件,附件取自子目录中的文件。

这是多步骤方法的一部分,我使用 imagettftext 获取模板 jpg 培训证书,用学习者数据填充它,然后将文件保存到服务器,我已经这样做了,例如:

    $filenamedate = date('Ymdhi');
    $file=$filenamedate." ".$name." (".$CourseDescription .")";
    $file_path="Certificates/".$file.".jpg";

成功将 .jpg 保存到 /Certificate 文件夹,下面的文件名是一个示例:

202303070135 乔纳森·格林(D1 司机政策和规则).jpg

我能够使用 mail_runner 发送一封基本的电子邮件,这对于这个项目来说是必须的,这包括普通字段,例如 to、msg、from 等。

但是,我正在努力让代码工作以附加上面保存的文件。我正在尝试大致:

$from = "[email protected]";
$to = "[email protected]";
$msg = "Test Documents Attached.";
$subject="Documents";
 
$attachments = array();
// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:
//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type
$attachments = array('path' => getabspath('$file_path'),
                  'name' => 'testcertificate.jpg',
                  'encoding' => 'base64',
                  'type' => 'application/octet-stream');
 
$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'body' => $msg, 'attachments' => $attachments));

不幸的是,这不起作用,并给了我以下错误:

致命错误:未捕获的 TypeError:无法访问字符串类型的偏移量 在字符串上

我所做的唯一其他修改导致电子邮件在附件中发送,但无法打开它。

那么,我需要做些什么才能从服务器中获取此文件并作为附件发送?

但是,我正在努力让代码工作以附加上面保存的文件。我正在尝试大致:

`$from = "[email protected]";
$to = "[email protected]";
$msg = "Test Documents Attached.";
$subject="Documents";
 
$attachments = array();
// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:
//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type
$attachments = array('path' => getabspath('$file_path'),
                  'name' => 'testcertificate.jpg',
                  'encoding' => 'base64',
                  'type' => 'application/octet-stream');
 
$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'body' => $msg, 'attachments' => $attachments));`

不幸的是,这不起作用,并给了我以下错误:

致命错误:未捕获的 TypeError:无法访问字符串类型的偏移量 在字符串上

我所做的唯一其他修改导致电子邮件在附件中发送,但无法打开它。

那么,我需要做些什么才能从服务器中获取此文件并作为附件发送?

PHP 电子邮件 附件

评论

0赞 ADyson 3/8/2023
500 内部服务器错误是一条一般错误消息,通知您服务器在处理请求时崩溃。除此之外,它(故意)毫无意义,对调试几乎没有用处。您需要检查服务器上的错误日志,以尝试查找基础异常消息。一旦你掌握了这一点,你就有机会识别问题,然后继续尝试解决它。
0赞 JKG79 3/8/2023
谢谢,我更新了它,它缺少一个;但现在我得到了致命错误:未捕获的 TypeError:无法访问 C 中字符串上字符串类型的偏移量
0赞 ADyson 3/8/2023
哪条线抛出的?你有没有用谷歌搜索过这个错误?你做过任何调试吗?听起来您正在尝试访问一个字符串(即文本变量),就好像它是一个数组一样。
1赞 cOle2 3/8/2023
变量两边有单引号,因此将尝试获取名为 的文件的路径。还应该是一个数组,每个文档:xlinesoft.com/phprunner/docs/runner_mail_function.htmgetabspath()$file_path$attachments

答: 暂无答案