Laravel 10 使用 EsmtpTransport 类测试电子邮件连接

huangapple go评论79阅读模式
英文:

Laravel 10 test email connection with EsmtpTransport class

问题

我尝试使用用户提供的配置测试电子邮件连接,但似乎找不到一种测试连接的方法。每次尝试使用TLS加密进行测试时都会出现错误。如果我将端口更改为SSL端口并将加密更改为SSL,它可以工作,但我也想让它与TLS一起工作。我注意到,即使端口设置为TLS使用的端口,加密设置为TLS,它仍然将"ssl://.."添加到URL中,我不知道这是否导致了问题或如何解决它。

我使用的是Laravel 10和Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport类。

真的很感激任何帮助。提前谢谢。

ssl://smtp.gmail.com:587": stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number

这是我尝试测试的方式:

// smtp.gmail.com
$host = Setting::where('key', 'email_host')->firstOrFail()->value;

// 587
$port = Setting::where('key', 'email_port')->firstOrFail()->value;

// my-username
$username = Setting::where('key', 'email_username')->firstOrFail()->value;

// my-app-password
$password = Setting::where('key', 'email_password')->firstOrFail()->value;

// TLS (or SSL)
$encryption = Setting::where('key', 'email_encryption')->firstOrFail()->value;

$tls = ($encryption == 'tls') ? true : false;

$transport = new EsmtpTransport($host, $port, $tls);
$transport->setUsername($username);
$transport->setPassword($password);

try { $transport->start(); }
catch (\Exception $e) { ... }
英文:

I am trying to test a email connection with a user given configuration but I can't seem to find a way to test the connection. I get an error everytime I try to test it with a TLS encryption. If I change the port to a SSL one and the encryption to SSL it works, but I want to get it working with TLS too. I noticed that even when the port is set to the one TLS uses and the encryption is set to TLS it still adds "ssl://.." to the URL, I have no idea if this is what causes the problem or how to solve it.

I am using Laravel 10 and the Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport class.

Really appreciate any help. Thanks in advance.

ssl://smtp.gmail.com:587": stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number

Here's how I try to test it:

// smtp.gmail.com
$host = Setting::where('key', 'email_host')->firstOrFail()->value;

// 587
$port = Setting::where('key', 'email_port')->firstOrFail()->value;

// my-username
$username = Setting::where('key', 'email_username')->firstOrFail()->value;

// my-app-password
$password = Setting::where('key', 'email_password')->firstOrFail()->value;

// TLS (or SSL)
$encryption = Setting::where('key', 'email_encryption')->firstOrFail()->value;

$tls = ($encryption == 'tls') ? true : false;

$transport = new EsmtpTransport($host, $port, $tls);
$transport->setUsername($username);
$transport->setPassword($password);

try { $transport->start(); }
catch (\Exception $e) { ... }

答案1

得分: 1

我认为参数$tls的命名可能会让人感到困惑。SSL和TLS是同义词。

当设置$tls = true时,确实会在主机前加上ssl://,表示使用SSL加密。在这里查看代码

你似乎尝试使用STARTTLS,这会根据端口和SSL是否关闭自动发生。

另请参阅https://talk.octobercms.com/t/smtp-starttls-and-mail-encryption-in-laravel-9/1041

因此,传递$tls = false应该可以完成任务并使用STARTTLS

英文:

I think the parameter $tls might be confusingly named.
SSL and TLS is synonym.

When setting $tls = true indeed ssl:// is prefixed to the host, meaning a SSL encryption is used. See here in the code.

What you seem to try to do is to use STARTTLS this happens automatically based on the port and if SSL is off.

See also https://talk.octobercms.com/t/smtp-starttls-and-mail-encryption-in-laravel-9/1041

So passing $tls = false should do the job and use STARTTLS.

huangapple
  • 本文由 发表于 2023年3月12日 19:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定