How to send batch/mass emails with attachments using mailgun?

梦想与她 提交于 2019-12-10 15:48:27

问题


I want to send batch emails with attachment. I can send the batch emails by attaching the same file to all the emails. But I need to attach different files to different emails by adding file path in recipient variables. I can't see anything related in the official documentation of mailgun.

Here is my code :

 # 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%')
    ));    

The above code does not work. the attachment array is not able to use recipient variable. Replacing %recipient.image% with /path/to/file works fine.


回答1:


As per conversation with the Mailgun support team, At this time Mailgun doesn't have a way to assign a specific attachment to each recipient. One thing that can be tried is serving the files on a server and assign the users a URL to retrieve the file from(This is suggested only in case if files are not confidential and stored permanently on the server.).




回答2:


From the doc:

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

OR

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

OR (inline):

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



来源:https://stackoverflow.com/questions/53700989/how-to-send-batch-mass-emails-with-attachments-using-mailgun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!