2017-05-07 161 views
1

我想使用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'); 
?> 
+1

请看这里,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) –

+0

噢好吧。所以我需要使用SendGrids PHP库。我已经下载了库,将sendgrid-php.php文件复制到我的根目录,并将以下代码行添加到我的php文件中: require(“sendgrid-php.php”); 这是你在说什么? – kfm1607

+1

是的。最重要的是,您还必须重构代码,因为没有任何PHPMailer相关的代码可以与SendGrid API一起使用。 –

回答

1

这里是一步一步的进程发送使用SendGrid API电子邮件,

  • Download Packaged Library下载存档SendGrid库。解压缩并将其放入项目目录中。
  • 转到https://app.sendgrid.com/settings/api_keys并为您的帐户/应用程序创建一个API密钥。
  • 现在来编码部分。重构代码以下列方式,

    require_once('sendgrid-php/sendgrid-php.php'); 
    
    $from = new SendGrid\Email(null, "SENDER'S_EMAIL_ADDRESS"); 
    $subject = "Hello World from the SendGrid PHP Library!"; 
    $to = new SendGrid\Email(null, "RECEIVER'S EMAIL ADDRESS"); 
    $content = new SendGrid\Content("text/plain", "Hello, Email!"); 
    $mail = new SendGrid\Mail($from, $subject, $to, $content); 
    
    $apiKey = 'YOUR_API_KEY'; 
    $sg = new \SendGrid($apiKey); 
    
    $response = $sg->client->mail()->send()->post($mail); 
    if($response->statusCode() == 202){ 
        echo "Email sent successfully"; 
    }else{ 
        echo "Email could not be sent"; 
    } 
    

    不要忘记更改SENDER'S_EMAIL_ADDRESSRECEIVER'S_EMAIL_ADDRESSYOUR_API_KEY按您的要求。最重要的是,根据您的应用程序的要求更改电子邮件的主题和正文。


旁注:万一有什么差错在中途,使用以下三种响应方式进行调试。

echo $response->statusCode(); 
var_dump($response->headers()); 
echo $response->body(); 
+0

非常感谢您的帮助!有效!!电子邮件发送成功并收到! 请最后一个问题。我希望从联系表单中提取电子邮件的内容。我怎么做?例如,我想要$ from = new SendGrid \ Email(null,“SENDER'S_EMAIL_ADDRESS”);从表单中提取电子邮件的值。 – kfm1607

+0

@Anonymous只需将变量代替'SENDER'S_EMAIL_ADDRESS',就是这样。这里有一个例子,'$ from = new SendGrid \ Email(null,$ Email);'。另外,如果解决了您的问题,请*接受*答案。 [如何接受Stack Overflow的答案?](https://meta.stackexchange.com/a/5235) –

+0

接受。非常感谢:) – kfm1607