如何在 Symfony 中使用 swiftmailer 使我的电子邮件用户名符合 RFC 2822?
How can I make my email username comply with RFC 2822 using swiftmailer in Symfony?
我尝试在 services.yaml
中为 swiftmailer 设置电子邮件配置
parameters:
mailer:
smtp_host: 'abc123.webthing.myhost.de'
smtp_port: 587
smtp_cert: 'tls'
smtp_username: 'ab12345-laura'
smtp_password: 'secret'
这是邮件服务:
<?php
/**
* Service that can be used to send email using basic swift mailer transport
*/
namespace App\Service;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class Mailer
{
/**
* @var array $emailConfig
*/
private $emailConfig;
/**
* Mailer constructor
*
* @param ParameterBagInterface $params
*/
public function __construct(ParameterBagInterface $params)
{
$this->emailConfig = $params->get('mailer');
}
public function sendMail(array $mailInfo, string $html, array $files = [])
{
$emailConfig = $this->emailConfig;
$smtpHost = $emailConfig['smtp_host'];
$smtpPort = $emailConfig['smtp_port'];
$smtpCert = $emailConfig['smtp_cert'];
$smtpUsername = $emailConfig['smtp_username'];
$smtpPassword = $emailConfig['smtp_password'];
$transport = (new \Swift_SmtpTransport($smtpHost, $smtpPort, $smtpCert))
->setUsername($smtpUsername)
->setPassword($smtpPassword)
;
$swiftMailer = new \Swift_Mailer($transport);
$message = (new \Swift_Message($mailInfo['title']))
->setFrom([$smtpUsername => $mailInfo['senderName']])
->setTo($mailInfo['sendTo'])
->setBody($html, 'text/html');
foreach ($files as $file) {
$message->attach(\Swift_Attachment::fromPath($file));
}
$swiftMailer->send($message);
}
}
我测试了这样的代码:
/**
* @Route("/_mailer", name="mailer", methods={"GET","POST"})
*/
public function mailer(MailGenerator $mailGenerator, Request $request) {
$output = $mailGenerator->sendMail();
return $output;
}
我收到错误消息:
Address in mailbox given [ab12345-laura] does not comply with RFC
2822, 3.6.2.
问题出在这里:
setFrom([$smtpUsername => $mailInfo['senderName']])
这需要发送邮件的人的电子邮件地址及其显示名称。您使用 $smtpUsername
作为电子邮件地址,但 ab12345-laura
不是电子邮件地址。
你需要这样的东西:
setFrom(["laura@mail.test" => "Laura Something"])
因此,您的 $mailInfo
不仅应包含姓名,还应包含用于发件人的电子邮件地址,然后使用
setFrom([$mailInfo['senderEMail'] => $mailInfo['senderName']])
问题可能出在这段代码上:
$message = (new \Swift_Message($mailInfo['title']))
->setFrom([$smtpUsername => $mailInfo['senderName']])
->setTo($mailInfo['sendTo'])
->setBody($html, 'text/html');
它试图将发件人 地址 设置为 ab12345-laura
,根据 RFC,这很明显不是有效的发件人地址,因为它缺少 @domain
部分
我尝试在 services.yaml
中为 swiftmailer 设置电子邮件配置parameters:
mailer:
smtp_host: 'abc123.webthing.myhost.de'
smtp_port: 587
smtp_cert: 'tls'
smtp_username: 'ab12345-laura'
smtp_password: 'secret'
这是邮件服务:
<?php
/**
* Service that can be used to send email using basic swift mailer transport
*/
namespace App\Service;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class Mailer
{
/**
* @var array $emailConfig
*/
private $emailConfig;
/**
* Mailer constructor
*
* @param ParameterBagInterface $params
*/
public function __construct(ParameterBagInterface $params)
{
$this->emailConfig = $params->get('mailer');
}
public function sendMail(array $mailInfo, string $html, array $files = [])
{
$emailConfig = $this->emailConfig;
$smtpHost = $emailConfig['smtp_host'];
$smtpPort = $emailConfig['smtp_port'];
$smtpCert = $emailConfig['smtp_cert'];
$smtpUsername = $emailConfig['smtp_username'];
$smtpPassword = $emailConfig['smtp_password'];
$transport = (new \Swift_SmtpTransport($smtpHost, $smtpPort, $smtpCert))
->setUsername($smtpUsername)
->setPassword($smtpPassword)
;
$swiftMailer = new \Swift_Mailer($transport);
$message = (new \Swift_Message($mailInfo['title']))
->setFrom([$smtpUsername => $mailInfo['senderName']])
->setTo($mailInfo['sendTo'])
->setBody($html, 'text/html');
foreach ($files as $file) {
$message->attach(\Swift_Attachment::fromPath($file));
}
$swiftMailer->send($message);
}
}
我测试了这样的代码:
/**
* @Route("/_mailer", name="mailer", methods={"GET","POST"})
*/
public function mailer(MailGenerator $mailGenerator, Request $request) {
$output = $mailGenerator->sendMail();
return $output;
}
我收到错误消息:
Address in mailbox given [ab12345-laura] does not comply with RFC 2822, 3.6.2.
问题出在这里:
setFrom([$smtpUsername => $mailInfo['senderName']])
这需要发送邮件的人的电子邮件地址及其显示名称。您使用 $smtpUsername
作为电子邮件地址,但 ab12345-laura
不是电子邮件地址。
你需要这样的东西:
setFrom(["laura@mail.test" => "Laura Something"])
因此,您的 $mailInfo
不仅应包含姓名,还应包含用于发件人的电子邮件地址,然后使用
setFrom([$mailInfo['senderEMail'] => $mailInfo['senderName']])
问题可能出在这段代码上:
$message = (new \Swift_Message($mailInfo['title']))
->setFrom([$smtpUsername => $mailInfo['senderName']])
->setTo($mailInfo['sendTo'])
->setBody($html, 'text/html');
它试图将发件人 地址 设置为 ab12345-laura
,根据 RFC,这很明显不是有效的发件人地址,因为它缺少 @domain
部分