php - mailgun - 发送带有附件的邮件

标签 php symfony email mailgun symfony-3.2

我试图在 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/

相关文章:

php - 使用 PHP XMLReader 对 XML 树中的所有属性进行排序

php - Symfony2/Doctrine 中两列的查询生成器和分组依据生成重复项

php - 当注销链接命中时,FOS UserBundle 服务器出现错误

android - 无法在 Android 中添加电子邮件签名

email - 如何为 Firebase 崩溃报告启用电子邮件警报

php - 如何在 Laravel 5.3 上的 Blade 文件内创建方法

php - 在自定义查询php中匹配用户密码

php - 使用 array_intersect 将数组与重复值进行比较?

symfony - 部署我的 Symfony 应用程序,出现 404 css 相关错误

django - 使用联系表单和 Zoho Mail 时出现 SMTPDataError(553,b'Relaying disallowed as abc@email.com')