Red Vulpine
Red Vulpine

Reputation: 453

PHPMailer sends emails but the client does not receive them

I have a problem in PHPMailer in particular. I have a contact form, and use PHPMailer to send emails. Apparently I can send, for me it returns "true", but the email does not arrive in your inbox. I've tried several ways to find the error, but without success. I tried enabling and disabling the SMTP with and without SMTP authentication, multiple forms. Follow my code below.

<?php
session_start();
require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

//$mail->IsSMTP();
$mail->Host = "smtp.mysite.com";
$mail->SMTPAuth = false; 
$mail->Username = '[email protected]';
$mail->Password = 'passhere';

$mail->From = "Newsletter";
$mail->Subject = "Newsletter";
$email = $_POST['email_news'];
$mail->AddAddress('[email protected]');
$mail->IsHTML(true);
$mail->CharSet = 'utf-8';

$mail->Body = "
    <html>
        <body>
        <b>Email:</b> $email<br/><br/>
        </body>
    </html>             
";

if(!$email){
    $result = "error";
}else{
    $send = $mail->Send();
    if($send){
        $result = "sucess";
    }else{
        $result = "error";
        echo $mail->ErrorInfo;
    }
}

$mail->ClearAllRecipients();
$mail->ClearAttachments();

header("Location: http://www.mysite.com");

?>

I managed to solve my problem using authentication via gmail. Was as follows authentication:

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = '[email protected]';
$mail->Password = 'passhere';

But I have a doubt. This authentication code is just for sending emails from gmail? Or is it an endorsement gmail provides for general validation email in PHPMailer?

Upvotes: 4

Views: 9715

Answers (1)

ArabianMaiden
ArabianMaiden

Reputation: 527

I would've commented but I don't have enough points.

This happened to me a couple weeks back. Just wanted to share that if you are using a general shared hosting plan, ask your web host to configure the domain to enable SPF. (My package did not come with these settings as default - just check with your web host server).

Some of the domains hosted on my webhost server were receiving emails while others were not (the emails were bounced back to sender even if they were sent by Gmail). I could not figure out where the issue was until someone suggested that if you're on a shared hosting plan, check your records.

You can also find topics about it on cPanel's forums https://forums.cpanel.net/threads/email-can-send-not-receive.594419/

Just my 2 cents, hope it helps someone.

Upvotes: 2

Related Questions