SendGrid / PHP – 为什么我发送和接收到重复的电子邮件?

huangapple go评论74阅读模式
英文:

SendGrid / PHP - Why am I sending and receiving duplicate emails?

问题

我尝试在PHP中使用SendGrid发送电子邮件。我正在使用SendGrid提供的示例代码,电子邮件正在发送,但每次发送时,收件箱中都会收到两封重复的电子邮件。没有日志错误或指示电子邮件被复制的迹象。我只调用了一次代码,但找不到任何原因为什么会收到两封重复的电子邮件。任何见解将不胜感激。

function sendEmaill(){
$email = new \SendGrid\Mail\Mail();
$email->setFrom("xxxx@yyyyy.com", "Testing");
$email->setSubject("Your Registration is confirmed");
$email->addTo("aaaaa@bbbbbbb.com", "Test");
$email->addContent("text/plain", "Your Registration for X is confirmed");
$email->addContent(
"text/html", "<strong>If you have any questions please contact XXXXX</strong>"
);

$apiKey = "XXXXXXXXXXXXXXXXXX"; // 用你实际的SendGrid API密钥替换

$sendgrid = new \SendGrid($apiKey);
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
header("Location: /account/registration/index.php?c=123");
exit();
}
英文:

I am attempting to use SendGrid to send emails in PHP. I am using the sample code provided by SendGrid and emails are sending, but each time I send I receive TWO duplicate emails in the inbox. There are no log errors or indication that the emails are being duplicated. I am only calling the code once and I can't find any reason why I would receive two duplicate email. Any insight would be much appreciated.

function sendEmaill(){
$email = new \SendGrid\Mail\Mail();
$email-&gt;setFrom(&quot;xxxx@yyyyy.com&quot;, &quot;Testing&quot;);
$email-&gt;setSubject(&quot;Your Registration is confirmed&quot;);
$email-&gt;addTo(&quot;aaaaa@bbbbbbb.com&quot;, &quot;Test&quot;);
$email-&gt;addContent(&quot;text/plain&quot;, &quot;Your Registration for X is confirmed&quot;);
$email-&gt;addContent(
&quot;text/html&quot;, &quot;&lt;strong&gt;If you have any questions please contact XXXXX&lt;/strong&gt;&quot;
);

$apiKey = &quot;XXXXXXXXXXXXXXXXXX&quot;; // Replace with your actual SendGrid API key

$sendgrid = new \SendGrid($apiKey);
try {
$response = $sendgrid-&gt;send($email);
print $response-&gt;statusCode() . &quot;\n&quot;;
print_r($response-&gt;headers());
print $response-&gt;body() . &quot;\n&quot;;
} catch (Exception $e) {
echo &#39;Caught exception: &#39; . $e-&gt;getMessage() . &quot;\n&quot;;
}
header(&quot;Location: /account/registration/index.php?c=123&quot;);
exit();
}

答案1

得分: 2

原来我是通过在浏览器中加载页面来测试这个函数的,这导致函数运行了两次。当我将函数单独包含在一个名为 "Functions" 的文件中时,它只发送一封电子邮件。希望这对某人有所帮助。

英文:

Turns out I was testing this function by loading it on page in my browser, which was causing the function to run twice. When I included the function separately (in an included "Functions" file), it only sends the email once. Hope this helps someone.

huangapple
  • 本文由 发表于 2023年6月15日 00:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定