How to send batch/mass emails with attachments usi

2019-06-27 05:10发布

问题:

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']]