使用PHP发送的Mailgun CURL批处理邮件

时间:2018-08-09 11:29:37

标签: php email curl mailgun php-curl

需要批量处理邮件,但只能在最后一封邮件上获取邮件。

我的代码是

    $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;

但是我只收到邮件sample2@mail.com

这是我从文档中获得的源代码

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

以下工作:

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