How to send email
In any web program development, sending emails is a classic task, and it is extremely complicated and full of pitfalls. A solution to sending emails without inventing the wheel is to use the SwiftmailerBundle, which leverages the power of the Swift Mailer class library. This bundle has been built into the Symfony Standard Edition framework.
Configuration ¶
To use Swift Mailer, you need to configure it so that it can be used on the mail server.
Instead of setting up/using your own email server, you may wish to choose an email hosting provider such as Mandrill, SendGrid, Amazon SES or other brands. They will provide you with an SMTP server, username and password (commonly known as keys), which are used in Swift Mailer configuration.
In the standard version of Symfony installation, some swiftmailer
configuration information has been included:
# app/config/config.yml swiftmailer: transport: '%mailer_transport%' host: '%mailer_host%' username: '%mailer_user%' password: '%mailer_password%'
<!-- app/config/config.xml --><?xml version="1.0" encoding="UTF-8" ?><container xmlns="symfony.com/schema/dic/services" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:swiftmailer="symfony.com/schema/dic/swiftmailer" xsi:schemaLocation="hsymfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd symfony.com/schema/dic/swiftmailer symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd"> <swiftmailer:config transport="%mailer_transport%" host="%mailer_host%" username="%mailer_user%" password="%mailer_password%" /> </container>
// app/config/config.php$container->loadFromExtension('swiftmailer', array( 'transport' => "%mailer_transport%", 'host' => "%mailer_host%", 'username' => "%mailer_user%", 'password' => "%mailer_password%",));
These values (such as %mailer_transport%
) are read from the parameters set in the parameters.yml file. You can modify these values in that file, or set them directly here.
The following configuration properties are available:
transport
(smtp
,mail
,sendmail
, orgmail
)username
password
host
- ##port
- encryption
(
tls, or
ssl)
- auth_mode
(
plain,
login, or
cram-md5)
- spool
- type
(How to sort messages (messages),
fileor
memoryare supported, refer to
How to send rolling emails) - path
(The path to store information)
- type
- delivery_address
(An email address used to send all emails)
- disable_delivery
(Set to true to completely turn off email delivery)
¶
The Swift Mailer class library works by creating, configuring, and then sendingSwift_Message objects. "mailer" is responsible for the actual sending of information, which can be obtained through the
mailer service. Overall, sending an email is pretty straightforward:
public function indexAction($name){ $message = \Swift_Message::newInstance() ->setSubject('Hello Email') ->setFrom('send@example.com') ->setTo('recipient@example.com') ->setBody( $this->renderView( // app/Resources/views/Emails/registration.html.twig 'Emails/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(...);}To achieve loose coupling, the email body (body) is stored in a template and rendered using the
renderView() method.
registration.html.twigThe template might look like this:
{ # app/Resources/views/Emails/registration.html.twig #}<h3>You did it! You registered!</h3> Hi {{ name }}! You're successfully registered. {# example, assuming you have a route named "login" #}{# 示例,假设你有一个名为"login"的路由 #}To login, go to: <a href="{{ url('login') }}">...</a>. Thanks! {# Makes an absolute URL to the /images/logo.png file #}{# 确保“/images/logo.png file”是一个绝对路径 #}<img src="{{ absolute_url(asset('images/logo.png')) }}">
$message The object supports many options, such as including attachments, adding HTML content, etc. . Fortunately, Swift Mailer covers
Creating Messages in its own documentation.