我想使用SendGrid SMTP服務從我的網站的聯繫表格發送電子郵件。表單上的提交按鈕只是吐出我的網站的空白版本,沒有電子郵件發送到我的收件箱。SendGrid SMTP與聯繫表格集成
這裏是PHP代碼:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
// SMTP & Sendgrid settings
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->Port = "465";
$mail->SMTPAuth = "true"; // Enable SMTP authentication
$mail->Username = "sendgrid username";
$mail->Password = "sendgrid password";
$mail->SMTPSecure = ''; // Enable encryption, 'ssl' also accepted
// Email headers and body
$mail->SetFrom("[email protected]");
$mail->AddAddress("[email protected]");
// Form fields
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Contact = $_POST['Contact'];
$Message = $_POST['Message'];
$mail->Subject = "Message from yoursite.com";
$mail->Body = "You have a new message from your contact form on site.com \n \n Name: $Name \n Email: $Email \n Contact: $Contact \n Message: $Message";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else {
echo 'Message has been sent.';
}
header('Location: mywebsite.com');
?>
請看這裏,require(「class.phpmailer.php」); $ mail = new PHPMailer();',如果你想使用SendGrid API,那你爲什麼在這裏使用PHPMailer庫?閱讀這裏的文檔,[https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html](https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html) –
噢好吧。所以我需要使用SendGrids PHP庫。我已經下載了庫,將sendgrid-php.php文件複製到我的根目錄,並將以下代碼行添加到我的php文件中: require(「sendgrid-php.php」); 這是你在說什麼? – kfm1607
是的。最重要的是,您還必須重構代碼,因爲沒有任何PHPMailer相關的代碼可以與SendGrid API一起使用。 –