我正在处理我的第一个 Symfony 5 项目,并且很难使用内置的MailerInterface
. 我之前在 Symfony 3 中使用过 Swift Mailer,之前从未遇到过类似的问题。
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
...
class SomeController extends AbstractController {
public someAction(Request $request, MailerInterface $mailer) {
...
$email = (new TemplatedEmail())
->from(new Address('address@example.com', 'My Symfony Mail'))
//->to($user->getEmail())
->to('receiver@example.com')
->subject('Subject')
->htmlTemplate('email.html.twig');
$mailer->send($email);
}
}
// .env
#MAILER_DSN=smtp://user:pass@smtp.example.com:25
MAILER_DSN=sendmail://default
如果MAILER_DSN
格式不正确,则会抛出异常并显示在 Symfony 调试器页面上,例如The "invaliddsn" mailer DSN must contain a scheme
……因此,配置的 DSN 似乎smtp://user:pass@smtp.example.com:25
是正确的。在其他邮件应用程序中使用相同的凭据、主机和端口是没有问题的。
但是,当使用此代码时,不会显示错误/异常,并且我根本没有收到任何邮件。当然,我已经仔细检查了日志(没有错误),接收者垃圾邮件文件夹(没有)。指定 SMTP 服务器或 sendmail 没有任何区别。
Symfony 文档只解释了如何处理异常,但在我的例子中没有抛出异常。
虽然在 Symfony 中还有很多关于邮件问题的其他问题,但其中大多数都涉及旧的 Swift Mailer 或其他特定问题。
如何确定邮件是已发送但未收到还是根本未发送?