php-swiftmailerHow to set an HTML body using Swiftmailer?
Using Swiftmailer, you can set an HTML body for an email message by using the setBody() method. This method takes a string argument which should contain the HTML code for the body of the email.
Example code
$message = (new \Swift_Message('My subject'))
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody('<h1>Hello World!</h1>', 'text/html');
The code above will create a message with the subject "My subject" from "[email protected]" to "[email protected]" with an HTML body containing the text "Hello World!"
Parts of the code:
\Swift_Message('My subject'): creates a new Swift_Message object with the subject "My subject".setFrom('[email protected]'): sets the sender of the message to "[email protected]".setTo('[email protected]'): sets the recipient of the message to "[email protected]".setBody('<h1>Hello World!</h1>', 'text/html'): sets the body of the message to the HTML code provided, with the content type set to "text/html".
Helpful links
More of Php Swiftmailer
- How to configure Swiftmailer for SMTP without authentication?
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to set timeout with Swiftmailer?
- How to get the response code when using Swiftmailer?
- How to set the reply to address in Swiftmailer?
- How to set the port for Swiftmailer?
- How to set the sender name in SwiftMailer?
- How to send multiple attachments with SwiftMailer?
- How to encrypt emails with SwiftMailer?
See more codes...