Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SendGrid SMTP与联系人表单的集成_Php_Email_Smtp_Sendgrid - Fatal编程技术网

Php SendGrid SMTP与联系人表单的集成

Php SendGrid SMTP与联系人表单的集成,php,email,smtp,sendgrid,Php,Email,Smtp,Sendgrid,我想使用SendGrid SMTP服务从我网站的联系人表单发送电子邮件。表单上的“提交”按钮只显示了我网站的空白版本,没有向我的收件箱发送电子邮件 以下是PHP代码: <?php require("class.phpmailer.php"); $mail = new PHPMailer(); // SMTP & Sendgrid settings $mail->IsSMTP(); $mail->Host = "smtp.sendgri

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

以下是使用SendGrid API发送电子邮件的分步过程

  • 从下载存档的SendGrid库。提取它并将其放在项目目录中
  • 转到并为您的帐户/应用程序创建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";
    }
    
    不要忘记根据您的要求更改
    发件人的电子邮件地址
    收件人的电子邮件地址
    您的API密钥
    。除此之外,根据应用程序的要求更改电子邮件的主题和正文


旁注:如果中途出现问题,请使用以下三种响应方法进行调试

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

请参见此处,
require(“class.phpmailer.php”)$mail=新的PHPMailer(),如果您想使用SendGrid API,那么为什么要在此处使用phpMail库?阅读这里的文档,哦,好的。所以我需要使用SendGrids PHP库。我已经下载了这个库,将sendgrid-php.php文件复制到我的根目录,并在我的php文件中添加了以下代码行:require(“sendgrid php.php”);你是这么说的吗?是的。除此之外,您还必须重构您的代码,因为与PHPMailer相关的任何代码都不能与SendGrid API一起使用。您能告诉我您正在使用的文档,以及您下载SendGrid库(即SendGrid-php.php文件)的地方吗?非常感谢您的帮助!!!成功了!!已成功发送和接收电子邮件!最后一个问题。我想从联系表中提取电子邮件的内容。我该怎么做?例如,我想要$from=newsendgrid\Email(null,“发件人的电子邮件地址”);若要从表单中提取电子邮件值。@Anonymous只需将变量替换为发件人的电子邮件地址即可。下面是一个示例,
$from=newsendgrid\Email(null,$Email)。此外,如果答案解决了您的问题,请接受。认可的。非常感谢:)@Anonymous不客气!很高兴我能帮忙。干杯!:-)我真的很抱歉再次打扰您,但是PHP变量不起作用:(我完全按照您所说的做了)。