Nikita Nepali
Nikita Nepali

Reputation: 21

Sending Mail using PHP : SMTP ERROR

I am trying to send mail using php. But it gave me error,

" SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
"Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"
"

So that I searched alot to find the problem. There I got a solution, that I need to change $mail->IsMail(); from $mail->IsSMTP();

I did it and Mail was sent... But when I checked my mail,

I got, "This message may not have been sent by: [email protected]"

Being a developer I understood that An email should not contain such lines or issues.

I want to know, Is it alright if Receiver is showing such line in Email? and if not that What should I do?

I mean what changes I should make in my code.

Here is my php code:

**

date_default_timezone_set('Etc/UTC');
include 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// $mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = 'senderPassword';
$mail->setFrom("[email protected]", 'sender name');
$mail->addReplyTo('[email protected]', '');
$mail->addAddress($receiver, '');
$mail->Subject = 'Welcome';
$mail->Body    = 'body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send())
{
    return "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    return array('flag' => "1"); 
}

**

Upvotes: 0

Views: 6539

Answers (2)

Synchro
Synchro

Reputation: 37730

isMail and isSMTP use two different sending mechanisms. isMail submits messages through the PHP mail() function, which delivers mail to a local mail server via a sendmail binary. This local mail server then tries to deliver the message to its ultimate recipient. It's possible that this local server will accept a message which is later rejected, and that will be too late for your script to know about.

With isMail:

script -> local mail server -> gmail

With isSMTP:

script -> gmail

With isMail you don't need to authenticate (localhost is usually allowed to relay), and the message is sent from your server to gmail. With isSMTP your message is sent from gmail to gmail, and it does require authentication.

When sending directly through gmail, you need to authenticate with gmail, and that has its own set of problems (that will be why your script is not working) covered thoroughly in the PHPMailer docs, examples and questions here on SO.

When sending via your server, you're saying that you are sending from a gmail user, but it's being sent by your server, not by a server listed in gmail's SPF record. This is forgery, which is why you are seeing the "This message may not have been sent by..." message. It would not say that if you sent from an address in your own domain.

The solution is to fix your gmail authentication and send directly through gmail. Base your code on the gmail example provided with PHPMailer, not the old, obsolete code you're using, and read the docs.

Upvotes: 1

Hari Darshan
Hari Darshan

Reputation: 1920

Here is the code which is use for mailing purpose. Try setting the SMTPDebug mode 3 and check the output.

$mail = new PHPMailer;

$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxxxxx';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'xxxxxxxxxxxxxxxxx';
$mail->addAddress(xxxxxxxxxxxxxx);
$mail->addReplyTo('xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx');

$mail->isHTML(true);

$mail->Subject = '';
$mail->Body    = "";
$mail->send();

Upvotes: -1

Related Questions