Search code examples
phpphpmailer

Send email using live server SMTP server through PHP Mailer not working


I am using PHPMailer v6 and trying to send mail from a shared server email. my codes :

require_once "vendor/autoload.php";
$mail = new PHPMailer;
$mail->SMTPDebug = 3;                               
$mail->isSMTP();                                     
$mail->Host = 'cp-ht-1.webhostbox.net';
$mail->SMTPAuth = true;                               
$mail->Username = '[email protected]'; 
$mail->Password = "**********";                           
$mail->SMTPSecure = "ssl";                           
$mail->Port = 465;                                   
$mail->From = "[email protected]";
$mail->FromName = "Full Name";
$mail->addAddress("[email protected]", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send()) 
{
echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
echo "Message has been sent successfully";
}

if i run the page it shows success message but no any mail in my inbox. on debug I got :

CLIENT: 250 Message denied for spoofing attempt via SMTP Auth

If I used my gmail details then its working fine but don't know what is the problem with the server mail details .I have searched so many articles but did not get any solution. Some people said to remove the following line but its not working..

$mail->isSMTP();

please help


Solution

  • As the error says it clearly you're trying to do email spoofing.

    CLIENT: 250 Message denied for spoofing attempt via SMTP Auth
    

    More information on email spoofing:

    Email spoofing is the forgery of an email header so that the message appears to have originated from someone or somewhere other than the actual source. Email spoofing is a tactic used in phishing and spam campaigns because people are more likely to open an email when they think it has been sent by a legitimate source.

    Solution: As I can see in your code you have set up $mail->From to a gmail account. which is not owned by your domain and will end up failing.

    Change $mail->SetFrom = "[email protected]"; to $mail->SetFrom = "[email protected]";

    You can add replyTo to "[email protected]" if you want replied to go to "[email protected]"

    Thanks.