【问题标题】:Sendgrid example php sending duplicate emailsSendgrid 示例 php 发送重复的电子邮件
【发布时间】:2017-12-14 18:40:48
【问题描述】:

我基本上是通过 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 封电子邮件。

【问题讨论】:

    标签: php email curl phpmailer sendgrid


    【解决方案1】:

    如果您通过浏览器运行此程序,则很可能您的插件会导致重复提交 - 从您的浏览器的角度来看,这看起来只会发生一次。如果发生这种情况,Web 日志也会向您显示。尝试在命令行中从 curl 调用它,以确保它只提交一次请求。

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多