问题
I have a problem send email using Yii, the problem is sometime it result in ssl timeout, but some time mail send is going well.
The error says something like this
Swift_TransportException
Connection to ssl://in-v3.mailjet.com:465 Timed Out
Here is the error image
After reading some article, it's looks like because socket is timeout so it should start socket again.
Here is my article that I read.
swiftmailer and Yii2 not working 1 out of 10 time
How to close Smtp connection in SwiftMailer
Here is my config in common\config\main-local.php
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@backend/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'in-v3.mailjet.com',
'username' => 'myUserName',
'password' => 'myS3cr3tP4ss',
'port' => '465',
'encryption' => 'ssl',
],
],
Here is how I send email in my controller
$message = Yii::$app->mail->compose();
if (Yii::$app->user->isGuest) {
$message->setFrom('from@domain.com');
} else {
$message->setFrom(Yii::$app->user->identity->email);
}
$message->setTo(Yii::$app->params['adminEmail'])
->setFrom(Yii::$app->params['adminEmail'])
->setTo($model->email)
->setSubject('Reset Password')
->setHtmlBody($this->renderAjax('//email/_content',['content' => 'email content goes here']))
->send();
return $this->render('confirmation_sent');
How I can stop and start connection with my above code?
I have already put this code exactly before $message
if (Yii::$app->mailer->getTransport()->isStarted()) {
Yii::$app->mailer->getTransport()->stop();
}
Yii::$app->mailer->getTransport()->start();
But it still show SSL time out
sometimes.
I ask about this problem to mailjet, and got this response
Hi, Thank you for your reply.
Ok, please try with port 443 with SSL or: 25,80, 2525 with no encryption, 587 and 588 with TLS.
Let me know if it works.
Already try all the port with given encryption but it's remain the same, sometime I meet Timed Out
UPDATED
Using try catch give me Failed to authenticate on SMTP server with username "a3027c30a1c1f6f60d2f9d7813360b46" using 3 possible authenticators1
some times.
Using try catch give me Connection to ssl://in-v3.mailjet.com:465 Timed Out
too, it show sometime.
Here is my traceroute from our server to mailjet
traceroute to in-v3.mailjet.com (37.59.74.234), 30 hops max, 60 byte packets
1 gateway (123.231.250.129) 1.084 ms 1.114 ms 3.485 ms
2 36.37.76.84 (36.37.76.84) 17.495 ms 17.497 ms 36.37.76.70 (36.37.76.70) 1 7.215 ms
3 36.37.77.68 (36.37.77.68) 30.878 ms 36.37.77.14 (36.37.77.14) 30.322 ms 3 2.661 ms
4 36.37.77.39 (36.37.77.39) 33.103 ms * *
5 * * *
6 * * *
7 * * gblx.as3549.ny.us (213.251.130.102) 362.627 ms
8 * * *
9 * * *
10 * * *
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
Is this because different Yii::$app->mailer
to start stop socket and Yii::$app->mail
to send email?
Am I right these error because ssl socket is stopped? Please explain me too how come this error appear.
Thanks in advance.
回答1:
You have an error because it can not connect to the smtp and the time for attempts is over. Try to change the port and encryption, maybe this will help you but I'm not sure. It depends on your n-v3.mailjet.com, what settings he needs, I do not know.
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@backend/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'in-v3.mailjet.com',
'username' => 'myUserName',
'password' => 'myS3cr3tP4ss',
'port' => '587',
'encryption' => 'ssl',
],
],
来源:https://stackoverflow.com/questions/42918420/how-to-solve-swift-transportexception-connection-to-ssl-timed-out-error