英文:
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->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"; // Replace with your actual SendGrid API key
$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();
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论