Reputation: 925
I have been going through a ton of stuff online but still can't get my 'Submit' button to send an email using PHPMailer and Sendgrid. Here's my code:
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();
}
?>
I am very new to this so I have probably made plenty of mistakes. I am using xampp and have installed PHPMailer and Sendgrid in the xampp vendor folder using Composer. I created a sendgrid account and created an API KEY but am unsure how to use it. I am also unsure about how I've linked the 'submit' button to contact.php in the HTML as well, I am getting 'cannot POST scripts/contact.php' error.
There's a few problems here so here are my questions:
Upvotes: 0
Views: 832
Reputation:
Try the following code
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 = '[email protected]'; // 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('[email protected]', 'XXXXXX');
$mail->addAddress('[email protected]', 'XXXXXX'); // Add a recipient
// $mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'XXXXXXXX');
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');
// 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..");
}
Upvotes: 1
Reputation: 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 = '[email protected]'; // 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('[email protected]', 'XXXXXX');
$mail->addAddress('[email protected]', 'XXXXXX'); // Add a recipient
// $mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'XXXXXXXX');
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');
// 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..");
}
Upvotes: 0