gpt4 book ai didi

php - Mailgun 发送带附件的邮件

转载 作者:可可西里 更新时间:2023-11-01 00:12:37 25 4
gpt4 key购买 nike

我正在尝试使用 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>',
'to' => 'example@example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));

$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);

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

参见文档 curl_file_create并查看 PHP < 5.5 的注释。

关于php - Mailgun 发送带附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com