【问题标题】:How to send batch/mass emails with attachments using mailgun?如何使用mailgun发送带有附件的批量/批量电子邮件?
【发布时间】:2019-05-11 01:50:52
【问题描述】:

我想发送带有附件的批量电子邮件。我可以通过将相同的文件附加到所有电子邮件来发送批量电子邮件。但是我需要通过在收件人变量中添加文件路径来将不同的文件附加到不同的电子邮件中。我在mailgun的官方文档中看不到任何相关内容。

这是我的代码:

 # Instantiate the client.
    $mgClient = new Mailgun('key-****');
    $domain = "foo.bar.com";

    # Make the call to the client.
    $result = $mgClient->sendMessage($domain, array(
        'from'    => 'gido@foo.baar.com',
        'to'      => array('user1@gmail.com', 'user2@gmail.com'),
        'subject' => 'Hi %recipient.first%',
        'text'    => 'Hey there, Just Testing',
        'recipient-variables' => '{"user1@gmail.com": {"first":"User1", "id":1, "file" : "/path/to/file1"},
                                   "user2@gmail.com": {"first":"User2", "id": 2, "file" : "/path/to/file2"}}'
    ), array(
        'attachment' => array('%recipient.file%')
    ));    

上面的代码不起作用。附件数组无法使用接收者变量。用/path/to/file 替换%recipient.image% 可以正常工作。

【问题讨论】:

  • 我不是 Mailgun 的人,但在附件中你使用 %recipient.image%,应该是 %recipient.file% 以匹配我假设的 recipient-variables 列表中的字段名称吗?跨度>
  • 你为什么希望 php 知道你所说的 '%recipient.image%' 是什么意思?
  • 没有看到文档或代码 sn-p 这样做,可以指向文档吗?
  • @NigelRen 感谢您指出这一点。实际上,这是发布问题时的打字错误。我现在已经修好了。
  • @MehiShokri 收件人变量可以通过%var% 在批量邮件中访问。您可以查看文档documentation.mailgun.com/en/latest/…

标签: php laravel email email-attachments mailgun


【解决方案1】:

根据与 Mailgun 支持团队的对话,目前 Mailgun 无法为每个收件人分配特定的附件。可以尝试的一件事是在服务器上提供文件并为用户分配一个 URL 以从中检索文件(仅在文件不是机密并永久存储在服务器上的情况下才建议这样做。)。

【讨论】:

    【解决方案2】:

    来自doc

    'attachment' => [['fileContent'=>$binaryFile,'filename'=>'test.jpg']]

    '附件' => [['filePath'=>'/tmp/foo.jpg','filename'=>'test.jpg']]

    OR(内联):

    'inline' => [['filePath'=>'/tmp/foo.jpg', 'filename'=>'test.jpg']]

    【讨论】:

    • 您分享的代码不适用于批量发送电子邮件。我想向所有用户发送不同的附件。附件对用户来说是唯一的。
    猜你喜欢
    • 2015-09-15
    • 2018-09-05
    • 2012-08-11
    • 2019-11-18
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2016-04-04
    • 2010-12-11
    相关资源
    最近更新 更多