Mail Exception only on server

时间:2015-10-06 08:27:08

标签: email smtp laravel-5 server production-environment

Stage

I have a web application which I deployed recently. Application needs to send emails for several reasons.

I am using yandex business email service for my domain, have many email accounts for my domain, and I can use those email addresses without any problem.

I have written some code to send email on my Laravel application, using Laravel's Mail::send. See it below at #2. And you can see my config/mail.php settings at #1 below.

I am testing my application on my windows computer, with WAMP server.

My production server is VPS server running Linux. PHP version 5.5.29.

Problem

Everything works perfectly on my local test environment; I can send mails, and they are delivered to hotmail, gmail or my domain's emails without any problem.

On the server however, I receive an error when I try to send an email. See #3 for error.

On the same server, I am able to send emails using PHPMailer with same email accounts, email settings and credentials, and without any problem.

Question and thoughts

Everything works on my local environment as expected and that makes me think it is a problem with my server configurations, PHP version, configuration or extensions, but I do not have the knowledge to judge that.

What have I tried and did not worked

Commented these lines to send email from default mail address. And also changed my default email address with another one, which is also working everywhere except on production server of this project;

//$transport = Mail::getSwiftMailer()->getTransport();
//$transport->setUsername($username);
//$transport->setPassword($password);

Disabled mail encryption. Set;

'encryption' => '',

Tried different driver;

'driver' => 'mail',

1) My config/mail.php

'driver' => 'smtp',
'host' => 'smtp.yandex.com',
'port' => 587,
'from' => ['address' => 'name@domain.com', 'name' => 'name'],
'encryption' => 'tls',
'username' => 'name@domain.com',
'password' => 'password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

2) Code I use

Mail::send($message, $data, function ($message) use ($account, $to, $subject) 
{
$acc = config("mail.accounts.$account");
$senderAddress = array_get($acc, 'address');

$username = array_get($acc, 'username', $senderAddress);
$password = array_get($acc, 'password', '');
$senderName = array_get($acc, 'name', $acc['address'], $senderAddress);

$replyToAddress = array_get($acc, 'replyTo.0', $senderAddress);
$replyToName = array_get($acc, 'replyTo.1', $senderName);

$message->from($senderAddress, $senderName)
    ->replyTo($replyToAddress, $replyToName)
    ->subject($subject)
    ->to($to);

$transport = Mail::getSwiftMailer()->getTransport();
$transport->setUsername($username);
$transport->setPassword($password);
});

Explanation for code above: This code basically allows me to send emails to any email account, and from many email accounts I have. And like I said, this works perfectly on my local test environment.

3) The Error

Swift_TransportException in AuthHandler.php line 181: Failed to authenticate on SMTP server with username "name@domain.com" using 2 possible authenticators

4) Working PHPMailer Script

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = "smtp.yandex.com";
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    $mail->Username = $sender;          // SMTP account username
    $mail->Password = $password;        // SMTP account password
    $mail->SetFrom($sender, 'Name');
    $mail->AddReplyTo($sender, 'Name');
    $mail->Subject = $subject;
    $mail->MsgHTML($message);
    $mail->AddAddress($to, $to);
    $mail->Send();

1 个答案:

答案 0 :(得分:0)

很难说出问题所在。或者为什么它适用于您的开发环境。但是快速谷歌搜索给了我smpt.yandex.com的以下设置:

  • 邮件服务器地址 - smtp.yandex.com;
  • 连接安全性 - SSL;
  • port - 465。

因此,它不适用于您的开发环境。它为什么起作用对我来说是一个谜。

请参阅:https://yandex.com/support/mail/mail-clients.xml