我已经在线浏览了很多东西,但是仍然无法使用“ 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”错误。
这里有一些问题,所以这是我的问题:
答案 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..");
}