I applied all the steps: I uploaded PHPMailer file to my htdocs folder. I let less secure apps use my Gmail account ( [email protected]) which is the same account I use for receiving messages from the website. I also created the php script to use PHPMailer. I am attaching this script to this post.
I think the problem is with the php I created. Could you please help me to fix it?
Many Thanks.
Hananneh
<?php
date_default_timezone_set('Etc/UTC');
// Edit this path if PHPMailer is in a different location.
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
/*
* Server Configuration
*/
$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = " [email protected]"; // Your Gmail address.
$mail->Password = "mypassword"; // Your Gmail login password or App Specific Password.
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
$errorMSG .= "Subject is required ";
} else {
$msg_subject = $_POST["msg_subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "[email protected]";
$Subject = "New Message Received";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\
";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\
";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\
";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\
";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
I have checked your script, and I see a few issues:
First of all, I see at the start of your script you initialize PHPmailer, but on the “send email” line, you don’t use PHPmailer. Instead, you send the message using the mail() function, which has no relation to PHPmailer.
Secondly, even if you would send the message with these settings, it will likely be rejected because of the From address you use. With Gmail, it’s best to use your Gmail address as the sending address. When using PHP mail, you must use a From address under your own domain.
Using the email address of the person who filled in the form is likely to get rejected by either the sending or receiving email provider. After all, their email provider does not whitelist you as an authorized sender, so they’ll think you are trying impersonator the contactor.
Thirdly, if the sending and receiving email address is the same, Gmail does not show the message in your inbox, only in the sent messages folder. That’s why it’s preferable to set up a special Gmail account for the contact form and have that message sent to another email address.
So, to summarize:
Update your script so it actually uses PHPmailer, not PHP mail(), to send the message.
Make sure the From address is an address you’re authorized to send from.
Make sure the message is sent to a different address than the From address.
As for the code, I think this would work:
// .... everything else above the mail(...) line.
// send email
$mail->setFrom('[email protected]', 'Your Executive Chair');
$mail->addAddress('[email protected]', 'Hannaneh');
$mail->addReplyTo($_POST["email"], $_POST["name"]); // This line ensures you can hit Reply from your inbox and have it send to the contactor.
$mail->Subject = $Subject;
$mail->Body = $Body;
if ($mail->send()) {
echo "Your message was sent successfully!";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
<?php
date_default_timezone_set('Etc/UTC');
// Edit this path if PHPMailer is in a different location.
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
/*
* Server Configuration
*/
$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = " [email protected]"; // Your Gmail address.
$mail->Password = "87411120Dallan"; // Your Gmail login password or App Specific Password.
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
$errorMSG .= "Subject is required ";
} else {
$msg_subject = $_POST["msg_subject"];
}
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\
";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\
";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\
";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\
";
// send email
$mail->setFrom('[email protected]', 'Your Executive Chair');
$mail->addAddress('[email protected]', 'Hannaneh');
$mail->addReplyTo($_POST["email"], $_POST["name"]); // This line ensures you can hit Reply from your inbox and have it send to the contactor.
$mail->Subject = $Subject;
$mail->Body = $Body;
if ($mail->send()) {
echo "Your message was sent successfully!";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>