dongzhen4180 2017-05-07 13:40
浏览 182
已采纳

SendGrid与联系表单的SMTP集成

I want to use SendGrid SMTP services to send emails from my website's contact form. 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

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 
 
 Name: $Name 
 Email: $Email 
 Contact: $Contact 
 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条回答 默认 最新

  • duanjingwei7239 2017-05-07 17:28
    关注

    Here's the step-by-step process to send emails using SendGrid API,

    • Download the archived SendGrid library from Download Packaged Library. 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.
    • 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. 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(); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程