使用SendGrid(PHP)设置CC param的多个电子邮件地址

时间:2016-08-04 21:12:34

标签: php json email curl sendgrid

我正在尝试将CC添加到多人的电子邮件中。我在documentation中看到,如果我创建一个带有电子邮件地址的数组,cc会附加,但这是不成功的。

这是我发送CC电子邮件的原因

$cc = array('email3@123.net', 'email4@123.net');
$params = array(
        'api_user'  => $user,
        'api_key'   => $password,

        'to'        => "email@123.net",                     
        'toname'    => 'To Name',
        'bcc'       => 'email2@123.net',
        'subject'   => $subject,
        'html'      => $content,
        'from'      => $from,
        'fromname'  => $fromname,
    );

    if(!empty($cc)){
        $params['cc'] =  json_encode($cc);
    }
    $url = $sendGrilUrl; 
    $request =  $url.'api/mail.send.json';
    $session = curl_init($request);
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_POSTFIELDS, $params);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);           
    $response = json_decode($response);

当我执行此方法时,我收到SendGrid发出的错误,指出“电子邮件地址无效”。

我已尝试过此link提供的方法。电子邮件被发送但它按原样打印字符串并且没有吸引力。

非常感谢一些帮助,如果需要更多细节,请告诉我。

1 个答案:

答案 0 :(得分:0)

您必须将电子邮件作为cc [0],cc [1]等数组值进行传递。请尝试以下代码段:

 $params = array(
    'api_user'  => $user,
    'api_key'   => $password,
    'to'        => "email@123.net",                     
    'toname'    => 'To Name',
    'bcc'       => 'email2@123.net',
    'cc[0]'     => 'email3@123.net',
    'cc[1]'     => 'email4@123.net',
    'subject'   => $subject,
    'html'      => $content,
    'from'      => $from,
    'fromname'  => $fromname,
);


$url = $sendGrilUrl; 
$request =  $url.'api/mail.send.json';
$session = curl_init($request);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);           
$response = json_decode($response);

现在应该可以使用! :)