我试图在 mailgun
的帮助下用 symfony 发送带有附件的邮件
我已经提到了这个 Question .还有这个 Reference .但没有帮助我。
这是我的功能,
public function sendMail($to, $subject, $body, $attachment = null)
{
$mgClient = new Mailgun($this->container->getParameter('mailgun_key'));
$domain = $this->container->getParameter('mailgun_domain');
$content = [
'from' => $this->container->getParameter('mailgun_from'),
'to' => 'tester <' . $to . '>',
'subject' => $subject,
'text' => $body
];
if (!empty($attachment)) {
$result = $mgClient->sendMessage("$domain", $content);
} else {
$result = $mgClient->sendMessage("$domain", $content, [
'attachment[0]' => [$attachment]
]);
}
return $result;
}
在附件中,我正在传递文件路径。即
/home/mypc/Downloads/test.txt
但我只收到空白邮件。附件不来了。
我哪里错了?请帮忙。
最佳答案
mailgun attachments documentation有以下信息:
附件
您可以从内存或通过文件路径附加文件。
从文件路径
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'Test file path attachments',
'text' => 'Test',
'attachment' => [
['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
]
]);
从内存里
// Some how load the file to memory
$binaryFile = '[Binary data]';
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'Test memory attachments',
'text' => 'Test',
'attachment' => [
['fileContent'=>$binaryFile, 'filename'=>'test.jpg']
]
]);
内嵌附件
$mg->messages()->send('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'Test inline attachments',
'text' => 'Test',
'inline' => [
['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']
]
]);
关于php - mailgun - 发送带有附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451487/