我可以通过PHPMailer发送电子邮件,但不能通过 Laravel。我假设我的 Laravel 配置是错误的。
我从我的本地开发环境发送。
Laravel 错误
Failed to authenticate on SMTP server with username "name@example.com" using 2 possible authenticators
Laravel 控制器
// ...
Mail::to('anyone@whatever.com')->send(new AnyEmailTemplate());
// ...
Laravel 配置/mail.php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.anyprovider.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'name@example.com'),
'name' => env('MAIL_FROM_NAME', 'example.com'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'name@example.com'),
'password' => env('MAIL_PASSWORD', 'supersecretpassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
]
Laravel .env
MAIL_DRIVER=smtp
MAIL_HOST=mail.anyprovider.com
MAIL_PORT=587
MAIL_USERNAME=name@example.com
MAIL_PASSWORD=supersecretpassword
MAIL_ENCRYPTION=tls
PHPMailer 脚本(有效)
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'mail.anyprovider.com';
$mail->SMTPAuth = true;
$mail->Username = 'name@example.com';
$mail->Password = 'supersecretpassword';
$mail->SMTPAutoTLS = false;
$mail->Port = 587;
$mail->CharSet = 'UTF-8';
$mail->setFrom('name@example.com', 'example.com');
$mail->addAddress('anyone@whatever.com');
$mail->isHTML(true);
$mail->Subject = 'Any subject';
$body = "Any content";
$altBody = "Any alternative content";
$mail->Body = $body;
$mail->AltBody = $altBody;
$mail->send();
知道如何根据使用的设置来配置 LaravelPHPMailer吗?
偶然的你
繁星淼淼