Search code examples
phpemailcurlmailgunphp-curl

Mailgun CURL batch mail send with PHP


Need to batch mail, but getting mail only on last one.

My code is

    $MailData            = array();
    $MailData['from']    = "John Doe <john@domain.com>";

    $MailData['to']      = 'sample1@gmail.com';
    $MailData['to']      = 'sample2@gmail.com';

    $MailData['recipient-variables']      = '{"sample1@gmail.com": {"first":"Bob", "id":1}, "sample2@gmail.com": {"first":"Alice", "id": 2}}';
    $MailData['subject'] = 'This is test thing';
    $MailData['text']    = 'How are you man';


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/'.$domain.'/messages');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $MailData);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;

But i'm getting mail only sample2@mail.com

This is the source code i got from documentation

curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
    -F to=alice@example.com \
    -F to=bob@example.com \
    -F recipient-variables='{"bob@example.com": {"first":"Bob", "id":1}, "alice@example.com": {"first":"Alice", "id": 2}}' \
    -F subject='Hey, %recipient.first%' \
    -F text='If you wish to unsubscribe, click http://mailgun/unsubscribe/%recipient.id%'

Solution

  • Try this

    $MailData['to'] = 'sample1@gmail.com, sample2@gmail.com';