加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Mailgun发送带附件的邮件

发布时间:2020-12-13 21:57:02 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用mailgun发送带附件的邮件. 邮件本身很好,但它缺少附件. 同样在mailgun日志中,它显示正常,但附件数组为空. 我用example.com替换了我的凭证. 该文件放在子目录中并且可读. $filename = 'directory/example.pdf';$parameters = array('from' = 'E
我正在尝试使用mailgun发送带附件的邮件.
邮件本身很好,但它缺少附件.
同样在mailgun日志中,它显示正常,但附件数组为空.

我用example.com替换了我的凭证.
该文件放在子目录中并且可读.

$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example@mail.example.com>','to' => 'example@example.com','subject' => 'Subject','text' => 'This is just a test.','attachment[1]' => '@' . $filename);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch,CURLOPT_USERPWD,'api:key-ThisIsJustARandomString');

curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch,CURLOPT_POSTFIELDS,$parameters);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$response = curl_exec($ch);

我没有收到错误,这是$response:

string(103) "{
    "id": "<20170514122638.8820.55203.4543B111@mail.example.com>","message": "Queued. Thank you."
}"

在mailgun日志中没有列出附件:

"message": {
    "headers": {
        "to": "example@example.com","message-id": "20170514122638.8820.55203.4543B111@mail.example.com","from": "Example <example@mail.example.com>","subject": "Subject"
    },"attachments": [],"size": 349
},

根据所有文档,我发现这将是正确的解决方案,但它不起作用.

提前感谢所有回复.

解决方法

将您的第一个代码更改为:

$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example@mail.example.com>','attachment[1]' => curl_file_create($filename,'application/pdf','example.pdf'));

$ch = curl_init();
curl_setopt($ch,true);

$response = curl_exec($ch);

我将’@’.$filename更改为curl_file_create($filename,’application / pdf’,’example.pdf’).

请参阅文档curl_file_create并查看PHP的说明< 5.5.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读