I'm using PHP Mailer in my website for my contact form.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/PHPMailer/src/Exception.php';
require 'phpmailer/PHPMailer/src/PHPMailer.php';
require 'phpmailer/PHPMailer/src/SMTP.php';
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$message = $_POST['textarea'];
$from = $_POST['email'];
$to = "support@domain.com";
$subject = 'email from website';
$mail = new PHPMailer();
$mail->Host = "smtp.hostinger.com";
$mail->SMTPDebug = 0;
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = $to;
$mail->Password = "mypassword";
$mail->SMTPSecure = "ssl";
$mail->From = $from;
$mail->FromName = $name;
$mail->addReplyTo($from, $name);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = "Sender": " . $name . " " . $lastname . " \n <br/> Phone: ". $phone ." \r\n <br/> message: " . $message";;
$mail->addAddress($to);
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;
}
else
{
echo 'success';
}
?>
Once I submit the contact form in my website the code above returns success
message.
No email is actually received. I have double checked all the $_POST
, the values are correct. Also double checked my username and password (I changed them in the question).
What can cause PHP Mailer to return success message and not send the email?
PHP Mailer debug
Sending with mail()<br>
Sendmail path: /usr/sbin/sendmail -t -i<br>
Envelope sender: <br>
To: support@domain.com<br>
Subject: =?UTF-8?B?15nXpteZ16jXqiDXp9ep16gg157XlNeQ16rXqA==?=<br>
Headers: Date: Sun, 19 Sep 2021 09:37:31 +0000From: test11 <test@gmail.com>Reply-To: test11 <test@gmail.com>Message-ID: <sK7qvbbNwmP2IikzBwV7c2mD3TqDUGwEOLeW2HZng@www.reboost.co.il>X-Mailer: PHPMailer 6.5.1 (https://github.com/PHPMailer/PHPMailer)MIME-Version: 1.0Content-Type: text/html; charset=UTF-8<br>
Result: true<br>
You need to tell phpMailer to use SMTP
$mail->isSMTP();
OP replied stating the following error:
Error sending: SMTP Error: The following recipients failed: support@domain.com: <test@gmail.com>: Sender address rejected: not owned by user support@domain.com
This can be caused by having the "from" field differ from the smtp login name.