在使用phpmailer和sendgrid设置直接电子邮件链接时遇到问题

时间:2019-02-11 11:34:23

标签: php html email xampp sendgrid

我已经在线浏览了很多东西,但是仍然无法使用“ Submit”按钮来使用PHPMailer和Sendgrid发送电子邮件。这是我的代码:

HTML

            <form id="contact-form" action="scripts/mailer.php" method="post">
                <fieldset form="#contact-form">
                    <legend>Contact Form</legend>
                    <label class="input-field-name">Name:<br />
                        <input class="input-field" type="text" name="name" required/>
                    </label><br />
                    <label class="input-field-name">Email:<br />
                        <input class="input-field" type="text" name="email" required/>
                    </label><br />
                    <label class="input-field-name">Message Title:<br />
                        <input class="input-field" type="text" name="header" required/>
                    </label><br />
                    <label class="input-field-name">Message:<br />
                        <textarea class="message-field" type="text" name="message" required></textarea>                            
                    </label><br />
                    <button id="submit-button" type="submit">Submit</button>
                </fieldset>
            </form>

PHP

<?php

$name = $_POST["name"];
$email = $_POST["email"];
$header = $_POST["header"];
$message = $_POST["message"];

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require "C:\xampp\composer\vendor\autoload.php";

$mail = new PHPMailer(TRUE);

try {
    $mail->setFrom($email, $name);
    $mail->addAddress("MY EMAIL", "MY NAME");
    $mail->Subject($header);
    $mail->isHTML(True);
    $mail->Body = ("<html>$message</html");
    $mail->AltBody = strip_tags($message);

    $mail->SMTPDebug = 1;
    $mail->isSMTP();
    $mail->Host = 'smtp.sendgrid.net';
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Username = "MY SENDGRID USERNAME";
    $mail->Password = "MY SENDGRID PASSWORD";
    $mail->Port = 587;

    $mail->SMTPOptions = array(
      'ssl' => array(
      'verify_peer' => false,
      'verify_peer_name' => false,
      'allow_self_signed' => true
      )
    );

    $mail->send();
} catch (Exception $e) {
    echo $e->errorMessage();
} catch (\Exception $e) {
    echo $e->getMessage();
}

?>

我对此很陌生,所以我可能犯了很多错误。我正在使用xampp,并已使用Composer在xampp供应商文件夹中安装了PHPMailer和Sendgrid。我创建了一个sendgrid帐户并创建了一个API KEY,但是不确定如何使用它。我也不确定如何将“提交”按钮也链接到HTML中的contact.php,我也收到“无法发布脚本/contact.php”错误。

这里有一些问题,所以这是我的问题:

  • PHP是否适合此过程?
  • 我是否已将PHP正确链接到“提交”按钮?
  • 如何避免出现“无法发布”错误?
  • 是否正在使用“ composer require sendgrid / sendgrid”从终端下载Sendgrid,然后将主机设置为smtp.sendgrid.net足以使它与Sendgrid一起运行?

2 个答案:

答案 0 :(得分:1)

尝试以下代码

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();  
$mail->SMTPDebug = 0;                                 // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'XXXXX@gmail.com';               // SMTP username
$mail->Password = 'XXXXXXXXX';                    // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                               // TCP port to connect to gmail 465/587/995/993

$mail->setFrom('XXXXX@gmail.com', 'XXXXXX');
$mail->addAddress('XXXXX@gmail.com', 'XXXXXX');     // Add a recipient
// $mail->addAddress('XXX@example.com');               // Name is optional
$mail->addReplyTo('XXX@gmail.com', 'XXXXXXXX');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
   // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Testing';

$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];

$mail->Body    = 'Name:'.$name ."<br/>".
                 'Email:' .$email."<br/>".
                 'Message:'  .$message;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo("Thank You..");
}

答案 1 :(得分:0)

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();  
$mail->SMTPDebug = 0;                                 // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'XXXXX@gmail.com';               // SMTP username
$mail->Password = 'XXXXXXXXX';                    // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                               // TCP port to connect to gmail 465/587/995/993

$mail->setFrom('XXXXX@gmail.com', 'XXXXXX');
$mail->addAddress('XXXXX@gmail.com', 'XXXXXX');     // Add a recipient
// $mail->addAddress('XXX@example.com');               // Name is optional
$mail->addReplyTo('XXX@gmail.com', 'XXXXXXXX');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
   // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Testing';

$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];

$mail->Body    = 'Name:'.$name ."<br/>".
                 'Email:' .$email."<br/>".
                 'Message:'  .$message;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo("Thank You..");
}