在 Symfony 3 中使用来自本地主机的 Gmail 发送电子邮件
Sending an email using Gmail from localhost in Symfony 3
我最近遇到了以下问题,我无法在 Symfony 3 中使用带有 Swiftmailer 的 Gmail 从我的本地主机发送任何邮件
这是我在 parameters.yml
中的设置
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: xxxx@gmail.com
mailer_password: xxxxx
mailer_encryption: tls
mailer_port: 587
这里是 config_dev.yml 设置
swiftmailer:
transport: gmail
username: '%mailer_user%'
password: '%mailer_password%'
和示例代码
/**
* @Route("/mail/{name}")
*/
public function mailAction($name)
{
echo 'tete';
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('konrad@mydomain.de')
->setTo('info@mydomain.de')
->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'email/registration.html.twig',
array('name' => $name)
),
'text/html'
)
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
),
'text/plain'
)
*/
;
$this->get('mailer')->send($message);
return $this->render('main/homepage.html.twig');
}
在我尝试发送一些测试邮件后,邮件只是假脱机,我没有看到任何错误或类似错误。
此外,我已经使用 PHPMailer 在本地使用上述设置在该库中进行了测试,发送工作正常。
我在本地工作时还需要设置更多吗?
Gmail 传输只是一个使用 SMTP 传输的快捷方式
并设置这些选项:
Option Value
encryption ssl
auth_mode login
host smtp.gmail.com
您错误地理解了这一点,因为您定义了传输 SMTP 参数,但实际上并没有使用它,因为配置查找 Gmail 传输,Symfony 自动配置了 Gmail SMTP 的默认值,您只需要提供用户名和参数中的密码。
# app/config/config_dev.yml
swiftmailer:
transport: gmail
username: '%mailer_user%'
password: '%mailer_password%'
%mailer_user%
和 %mailer_password%
预计将在
中定义
# app/config/parameters.yml
parameters:
# ...
mailer_user: your_gmail_username
mailer_password: your_gmail_password
我认为您没有正确配置,因为 parameters.yml
没有 mailer_user
,mailer_password
如果有,您已经覆盖了 Symfony 的 Gmail 默认设置使用或不正确。
无论哪种方式,请查看配置并仅填写适当的值。
phpmailer 也无关紧要,Symfony 使用 Swift 邮件程序。它与 phpmailer 一起工作,因为您可能正确设置了参数,但是 Swift 邮件程序读取配置 .yml
以生成传输对象,如果配置错误,传输对象值将是错误的。
(代表OP发表).
上面的代码按预期运行。即使没有指定加密或端口甚至主机。邮件到达我的邮箱,但被我的 SpamAssassin 设置过滤掉了。问题解决了。很抱歉从无到有地制造问题 :)
我最近遇到了以下问题,我无法在 Symfony 3 中使用带有 Swiftmailer 的 Gmail 从我的本地主机发送任何邮件
这是我在 parameters.yml
中的设置mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: xxxx@gmail.com
mailer_password: xxxxx
mailer_encryption: tls
mailer_port: 587
这里是 config_dev.yml 设置
swiftmailer:
transport: gmail
username: '%mailer_user%'
password: '%mailer_password%'
和示例代码
/**
* @Route("/mail/{name}")
*/
public function mailAction($name)
{
echo 'tete';
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('konrad@mydomain.de')
->setTo('info@mydomain.de')
->setBody(
$this->renderView(
// app/Resources/views/Emails/registration.html.twig
'email/registration.html.twig',
array('name' => $name)
),
'text/html'
)
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
),
'text/plain'
)
*/
;
$this->get('mailer')->send($message);
return $this->render('main/homepage.html.twig');
}
在我尝试发送一些测试邮件后,邮件只是假脱机,我没有看到任何错误或类似错误。
此外,我已经使用 PHPMailer 在本地使用上述设置在该库中进行了测试,发送工作正常。
我在本地工作时还需要设置更多吗?
Gmail 传输只是一个使用 SMTP 传输的快捷方式 并设置这些选项:
Option Value
encryption ssl
auth_mode login
host smtp.gmail.com
您错误地理解了这一点,因为您定义了传输 SMTP 参数,但实际上并没有使用它,因为配置查找 Gmail 传输,Symfony 自动配置了 Gmail SMTP 的默认值,您只需要提供用户名和参数中的密码。
# app/config/config_dev.yml
swiftmailer:
transport: gmail
username: '%mailer_user%'
password: '%mailer_password%'
%mailer_user%
和 %mailer_password%
预计将在
# app/config/parameters.yml
parameters:
# ...
mailer_user: your_gmail_username
mailer_password: your_gmail_password
我认为您没有正确配置,因为 parameters.yml
没有 mailer_user
,mailer_password
如果有,您已经覆盖了 Symfony 的 Gmail 默认设置使用或不正确。
无论哪种方式,请查看配置并仅填写适当的值。
phpmailer 也无关紧要,Symfony 使用 Swift 邮件程序。它与 phpmailer 一起工作,因为您可能正确设置了参数,但是 Swift 邮件程序读取配置 .yml
以生成传输对象,如果配置错误,传输对象值将是错误的。
(代表OP发表).
上面的代码按预期运行。即使没有指定加密或端口甚至主机。邮件到达我的邮箱,但被我的 SpamAssassin 设置过滤掉了。问题解决了。很抱歉从无到有地制造问题 :)