简体   繁体   English

mailgun 向多个用户发送电子邮件

[英]mailgun send email to multiple users

i have the following mailgun.php file:我有以下 mailgun.php 文件:

define('MAILGUN_URL', 'https://api.eu.mailgun.net/v3/my_domain');
define('MAILGUN_KEY', 'xxxxxxxxx'); 
function sendmailbymailgun($to,$toname,$mailfromnane,$mailfrom,$subject,$html,$text,$tag,$replyto){
    $array_data = array(
        'from'=> $mailfromnane .'<'.$mailfrom.'>',
        'to'=>$toname.'<'.$to.'>',
        'subject'=>$subject,
        'html'=>$html,
        'text'=>$text,
        'o:tracking'=>'yes',
        'o:tracking-clicks'=>'yes',
        'o:tracking-opens'=>'yes',
        'o:tag'=>$tag,
        'h:Reply-To'=>$replyto
    );
    $session = curl_init(MAILGUN_URL.'/messages');
    curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($session);
    curl_close($session);
    $results = json_decode($response, true);
    return $results;
}

I am trying to send a notification email to end users with the following code: $eml = email address variable $usrn = user name $htm = html text generated from mailtext.php with displaytext function $cust_orderid = parameter,not really important in this case我正在尝试使用以下代码向最终用户发送通知电子邮件: $eml = 电子邮件地址变量$usrn = 用户名$htm = 从 mailtext.php 生成的带有 displaytext 函数的 html 文本$cust_orderid = 参数,在这方面并不重要案件

if ((strlen($eml)>5)&&(isset($eml))&&(!is_null($eml))) {
include('./mailtext.php');
$htm = displaytext($cust_orderid);       
require_once('./mailgun.php');
sendmailbymailgun($eml,$usrn,'NOTIFICATIONS','notify@mydomain.com','NOTIFICATION SUBJECT',$htm,'','','');
          }

everything works fine, until i have in $eml field multiple email addresses separated with comma.一切正常,直到我在$eml字段中有多个用逗号分隔的电子邮件地址。 then i got the following error message: 'to' parameter is not a valid address.然后我收到以下错误消息:“to”参数不是有效地址。 please check documentation请检查文件

i checked the documentation and it tells, that i can have multiple email addresses separated by comma.我检查了文档,它告诉我,我可以有多个用逗号分隔的电子邮件地址。 reference: mailgun API documentation参考: mailgun API文档

some ideas how to solve this problem?一些想法如何解决这个问题?

thanks in advance提前致谢

So after a suggestion of @Igor Ilic i modified the mailgun.php like this:因此,在@Igor Ilic 的建议下,我修改了 mailgun.php,如下所示:

<?
define('MAILGUN_URL', 'https://api.eu.mailgun.net/v3/mydomain');
define('MAILGUN_KEY', 'xxxxxxxxxxx'); 
function sendmailbymailgun($to,$toname,$mailfromnane,$mailfrom,$subject,$html,$text,$tag,$replyto){
    $array_data = array(
        'from'=> $mailfromnane .'<'.$mailfrom.'>',
        'to'=> $to,
        'subject'=>$subject,
        'html'=>$html,
        'text'=>$text,
        'o:tracking'=>'yes',
        'o:tracking-clicks'=>'yes',
        'o:tracking-opens'=>'yes',
        'o:tag'=>$tag,
        'h:Reply-To'=>$replyto
    );
    $session = curl_init(MAILGUN_URL.'/messages');
    curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($session);
    curl_close($session);
    $results = json_decode($response, true);
    return $results;
}
?>

the only change is:唯一的变化是:

'to'=> $to,

instead of:代替:

'to'=>$toname.'<'.$to.'>',

now i am simply ignoring the < > part of the email sending - it's not so pretty with the display name/email address in the email header, but i don't care, it works, even when the email address $eml parameter looks dirty like this: 'email1@gmail.com,,,email2@yahoo.com,,email3@office.com,,'现在我只是忽略了电子邮件发送的 < > 部分——电子邮件标题中的显示名称/电子邮件地址不太漂亮,但我不在乎,它有效,即使电子邮件地址$eml参数看起来很脏像这样: 'email1@gmail.com,,,email2@yahoo.com,,email3@office.com,,'

thanks for the great idea, problem solved谢谢你的好主意,问题解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM