我可以将 Amazon SES 与 Symfony2 和 Swiftmailer Bundle 一起使用吗?

发布于 2025-01-03 14:42:23 字数 206 浏览 1 评论 0原文

我在 Amazon 的 ec2 上托管一个网站,运行 64 位版本的 CentOS。

该网站有一个简单的联系我们表单,提交后需要向多个地址发送电子邮件(非常基本)。

有人使用过带有 Symfony2 和 Swiftmailer Bundle 的 Amazon SES 吗?如果是这样,您是否建议使用 SES 或更传统的电子邮件服务器来执行此类任务?

I'm hosting a site on Amazon's ec2 running a 64-bit version of CentOS.

The site has a simple Contact Us form that needs to send an email to several addresses when submitted (pretty basic).

Has anyone used Amazon's SES with Symfony2 and the Swiftmailer Bundle? And if so, do you recommend using SES or a more traditional email server for this type of task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月下伊人醉 2025-01-10 14:42:23

可以使用 swiftmailer 库附带的本机 SMTP 传输通过 SES 发送电子邮件。以下示例使用版本 4.2.2 进行测试。

Amazon SES 需要使用 TLS 加密

通过传递 tls 作为第三个构造函数参数,可以将 Swift_SmtpTransport 传输类配置为使用 TLS 加密:

require_once './vendor/swiftmailer/swiftmailer/lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance(
        'email-smtp.us-east-1.amazonaws.com', 
        25, 
        'tls'
    )
    ->setUsername('AWS_ACCESS_KEY')
    ->setPassword('AWS_SECRET_KEY')
;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
    ->setFrom(array('[email protected]'))
    ->setTo(array('[email protected]' => 'John Doe'))
    ->setBody('Here is the message itself')
;

// Send the message
$result = $mailer->send($message);

在 Symfony2 中,您可以将 swiftmailer 服务配置为使用 TLS 加密:

# app/config/config.yml
swiftmailer:
    transport:  smtp
    host:       email-smtp.us-east-1.amazonaws.com
    username:   AWS_ACCESS_KEY
    password:   AWS_SECRET_KEY
    encryption: tls

直接从 EC2 实例上安装的邮件服务器发送电子邮件不太可靠,因为 EC2 IP 地址可能已列入黑名单。建议使用受信任的邮件服务器,因此使用 SES 似乎是一个好主意。

It is possible to send email via SES with the native SMTP transport shipped with the swiftmailer library. Examples below were tested using version 4.2.2.

Amazon SES requires usage of TLS encryption.

Swift_SmtpTransport transport class can be configured to use TLS encryption by passing tls as the third constructor argument:

require_once './vendor/swiftmailer/swiftmailer/lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance(
        'email-smtp.us-east-1.amazonaws.com', 
        25, 
        'tls'
    )
    ->setUsername('AWS_ACCESS_KEY')
    ->setPassword('AWS_SECRET_KEY')
;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
    ->setFrom(array('[email protected]'))
    ->setTo(array('[email protected]' => 'John Doe'))
    ->setBody('Here is the message itself')
;

// Send the message
$result = $mailer->send($message);

In Symfony2, you can configure the swiftmailer service to use TLS encryption:

# app/config/config.yml
swiftmailer:
    transport:  smtp
    host:       email-smtp.us-east-1.amazonaws.com
    username:   AWS_ACCESS_KEY
    password:   AWS_SECRET_KEY
    encryption: tls

Sending emails directly from a mailserver installed on an EC2 instance is not very reliable as EC2 IP addresses may be blacklisted. It is recommended to use a trusted mailserver so using SES seems to be a good idea.

木有鱼丸 2025-01-10 14:42:23

通过 Symfony2 通过 SES 发送邮件对我来说并不是开箱即用的,因为我在 config.yml 中配置了假脱机选项

我偶然发现的另一个问题是端口。端口 25 和 587 工作完美,但 465 让我超时。

使用正确的 SMTP 服务器非常重要,起初我使用的是 us-east-1 (因为我从示例中复制了它),尽管我的 SMTP 实际上是 email-smtp.eu-west-1.amazonaws.com

所以这是我当前的配置:

parameters:
    mailer_transport: smtp
    mailer_host: email-smtp.eu-west-1.amazonaws.com
    mailer_user: AWS_ACCESS_KEY
    mailer_password: AWS_SECRET_KEY
    mailer_encryption: tls
    mailer_port: 587

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    encryption: "%mailer_encryption%"
    port: %mailer_port%
    auth_mode:  login

我通过在命令行上执行以下命令发现了问题:

php app/console swiftmailer:debug

Sending mails through SES via Symfony2 didn't work out of the box for me because I had the spool option configured in my config.yml.

Another problem I stumbled upon was the port. Port 25 and 587 work perfect but 465 got me a timeout.

And it's important that you are using the right SMTP server, at first I was using us-east-1 (because I copied it from an example) although my SMTP actually was email-smtp.eu-west-1.amazonaws.com

So here's my current config:

parameters:
    mailer_transport: smtp
    mailer_host: email-smtp.eu-west-1.amazonaws.com
    mailer_user: AWS_ACCESS_KEY
    mailer_password: AWS_SECRET_KEY
    mailer_encryption: tls
    mailer_port: 587

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    encryption: "%mailer_encryption%"
    port: %mailer_port%
    auth_mode:  login

I found the problem by executing the following on my command line:

php app/console swiftmailer:debug
握住你手 2025-01-10 14:42:23

有一个为 swiftmailer 预先构建的 SES 传输。设置非常容易:

https://github.com/jmhobbs/Swiftmailer-Transport- -AWS-SES

There's an SES transport prebuilt for swiftmailer. Very easy to set up:

https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

护你周全 2025-01-10 14:42:23

如果您可以坚持免费套餐限制(每日 2K 条消息),我绝对建议您坚持使用 SES 而不是传统的电子邮件服务器。它简单、易于与大多数平台集成,并且您可以消除电子邮件服务器的维护和运营成本(虽然很小,但它们仍然存在)。当然,使用 SES 时仍然存在数据传输费用,如您在 Amazon SES 定价,但这也可能适合您的需求。

If you can stick with the free tier limits (2K daily messages), I'd definitely recommend you to stick with SES instead of a traditional email server. It's simple, easy to integrate with most platforms, and you eliminate the maintenance and operation costs (although small, they are still there) for your email server. Of course, there are still data transfer costs when using SES, as you can see on Amazon SES pricing, but that might fit your needs as well.

娇俏 2025-01-10 14:42:23

自 2011 年 12 月起,您可以将 smtp 与 switfmail 一起使用,但在此之前问题是该捆绑包仍然没有通过 EC2 工作的实现,但已经存在。如果您喜欢使用 switfmail 等框架发送电子邮件,您应该拥有密码和密钥,然后执行以下操作:

 require_once 'lib/swift_required.php';

  //Create the Transport
  $transport = new Swift_AWSTransport(
    'AWS_ACCESS_KEY',
    'AWS_SECRET_KEY'
  );

  //Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);

  //Create the message
  $message = Swift_Message::newInstance()
  ->setSubject("What up?")
  ->setFrom(array('[email protected]'))
  ->setTo(array('[email protected]'))
  ->setBody("

要获取密钥,请进入 AWS 管理控制台”>“SMTP 设置”>“创建我的 SMTP 凭证

”需要安装此扩展:

https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

但我重复一遍这只是信息现在,您应该先在 AWS 管理控制台中验证您的电子邮件帐户。稍后应该可以工作。

Since december 2011 you can use smtp with switfmail but before The problem was that this bundle still don't have the implementation for work over EC2, but already exists. If you like send emails with some framework like switfmail you should have your password and key, and do something like this:

 require_once 'lib/swift_required.php';

  //Create the Transport
  $transport = new Swift_AWSTransport(
    'AWS_ACCESS_KEY',
    'AWS_SECRET_KEY'
  );

  //Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);

  //Create the message
  $message = Swift_Message::newInstance()
  ->setSubject("What up?")
  ->setFrom(array('[email protected]'))
  ->setTo(array('[email protected]'))
  ->setBody("

For take your key go inside AWS Management Console" > "SMTP Settings" > "create my SMTP credentials"

And you are going to need install this extension :

https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES

but I repet this is only information. Now, you should verified your email account before in your AWS Management Console and later should work.

仅此而已 2025-01-10 14:42:23

在较新的 Symfony 版本中,包含了对 SES 的支持。您只需传递您的凭据并在配置中设置您的 stmp 主机即可。

请参阅 Symfony 3.4Symfony 4.X

On more recent Symfony versions, support for SES is included. You can simply pass your credentials and set your stmp host in configuration.

See documentation for Symfony 3.4 and for Symfony 4.X

趁微风不噪 2025-01-10 14:42:23

只需添加“tls”作为第三个参数即可。工作正常
前任:

// Create the Transport
$transport = (new Swift_SmtpTransport('amazon-url', 587, 'tls'))
->setUsername('awsusernamexxxxxx')
->setPassword('awspasswordxxxxxx');
// Other codings
?>

Just add 'tls' as third paramater. Works fine
Ex:

// Create the Transport
$transport = (new Swift_SmtpTransport('amazon-url', 587, 'tls'))
->setUsername('awsusernamexxxxxx')
->setPassword('awspasswordxxxxxx');
// Other codings
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文