简体   繁体   English

SendGrid SMTP与联系表单的集成

[英]SendGrid SMTP integration with contact form

I want to use SendGrid SMTP services to send emails from my website's contact form. 我想使用SendGrid SMTP服务从网站的联系表发送电子邮件。 The submit button on the form just spits out a blank version of my website, with no emails sent to my inbox. 表单上的“提交”按钮只会吐出我网站的空白版本,没有电子邮件发送到我的收件箱。

Here's the PHP code: 这是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("abc@123.com");
    $mail->AddAddress("abc@123.com");

    // 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');
 ?>

Here's the step-by-step process to send emails using SendGrid API, 这是使用SendGrid API发送电子邮件的分步过程,

  • Download the archived SendGrid library from Download Packaged Library . 下载打包的库中下载存档的SendGrid库。 Extract it and put it in your project directory. 解压缩并将其放在您的项目目录中。
  • Go to https://app.sendgrid.com/settings/api_keys and create an API key for your account/application. 转到https://app.sendgrid.com/settings/api_keys并为您的帐户/应用程序创建API密钥。
  • Now comes to the coding part. 现在到了编码部分。 Refactor your code in the following way, 通过以下方式重构代码,

     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"; } 

    Don't forget to change SENDER'S_EMAIL_ADDRESS , RECEIVER'S_EMAIL_ADDRESS and YOUR_API_KEY as per your requirement. 不要忘记根据您的要求更改SENDER'S_EMAIL_ADDRESSRECEIVER'S_EMAIL_ADDRESSYOUR_API_KEY On top of this, change the subject and body of the email as per your application's requirement. 最重要的是,根据您的应用程序要求更改电子邮件的主题和正文。


Sidenote: In case anything goes wrong in the midway, use the following three response methods for debugging. 旁注:万一中途出现问题,请使用以下三种响应方法进行调试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM