【问题标题】:mailgun send email to multiple usersmailgun 向多个用户发送电子邮件
【发布时间】:2020-01-24 23:00:09
【问题描述】:

我有以下 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;
}

我正在尝试使用以下代码向最终用户发送通知电子邮件: $eml = 电子邮件地址变量 $usrn = 用户名 $htm = 使用 displaytext 函数从 mailtext.php 生成的 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,'','','');
          }

一切正常,直到我在$eml 字段中有多个用逗号分隔的电子邮件地址。 然后我收到以下错误消息: 'to' 参数不是有效地址。请检查文档

我检查了文档,它告诉我,我可以有多个用逗号分隔的电子邮件地址。参考: mailgun API documentation

一些想法如何解决这个问题?

提前致谢

【问题讨论】:

  • 如果您有一个 ,(逗号)分隔的电子邮件列表,您的 to 应该看起来有点不同。您不能只有一组 &lt; &gt; 您需要在列表中的每个电子邮件地址周围都有它。
  • 我可以忽略'toname'参数吗?这意味着我将只发布 email1@domain.com,email2@domain.com 而没有 ?
  • 旁注:您不需要在if 语句中的每个表达式周围都使用所有()。它可以重写为:if (strlen($eml) &gt; 5 &amp;&amp; isset($eml) &amp;&amp; !is_null($eml)),这使得它更加更具可读性。
  • 请再次检查我的示例。我意识到我使用“表达式”而不是“条件”。所有条件都有一对括号,而不是每个条件。所以你可以用if ($foo == 1 &amp;&amp; $bar == 2)代替if (($foo == 1) &amp;&amp; ($bar == 2))。如果你有类似的东西,你只需要对条件进行分组:if ($foo == 1 &amp;&amp; ($bar == 1 || $bar == 2))。 Afaik,这与例如 C# 没有区别

标签: php mailgun


【解决方案1】:

所以在@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;
}
?>

唯一的变化是:

'to'=> $to,

代替:

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

现在我只是忽略了电子邮件发送的 部分 - 电子邮件标题中的显示名称/电子邮件地址不是那么漂亮,但我不在乎,它有效,即使电子邮件地址 @987654324 @ 参数看起来像这样脏:'email1@gmail.com,,,email2@yahoo.com,,email3@office.com,,'

感谢您的好主意, 问题解决了

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多