Sendgrid示例php发送重复的电子邮件

时间:2017-07-10 21:38:48

标签: php email curl phpmailer sendgrid

我基本上是通过Sendgrid的示例来发送电子邮件到重复的电子邮件地址,但结果却很奇怪。我每次都收到重复的电子邮件(其中4封)。我查看了几个线程,几乎所有线程都已通过重复请求解决。但是,从我可以看到没有(并且,这基本上是来自Sendgrid的示例脚本)。任何帮助将不胜感激......

代码:

$url = 'https://api.sendgrid.com/';
$user = 'My Username';
$pass = 'My Password';

$json_string = array(

  'to' => array(
    'myemail@example.com', 'myemail@gmail.com'
  ),
  'category' => 'test_category'
);


$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'x-smtpapi' => json_encode($json_string),
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'senderemail@example.com',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);



// print everything out
print_r($response);

打印输出表示成功(仅一次)。电子邮件发送正常。唯一的问题是每封电子邮箱都会显示4封电子邮件。

1 个答案:

答案 0 :(得分:0)

如果你通过浏览器运行它,很可能你的插件会导致重复提交 - 这看起来只是从浏览器的角度发生一次。 Web日志还会显示是否发生这种情况。尝试在命令行上从curl调用它,以保证它只提交一次请求。