Sending an HTML email using Swift
Asked Answered
J

1

17

I would like to send an auto-generated email with HTML body from my application using Swift.

Here is my current code:

$message = Swift_Message::newInstance()
                ->setFrom(array('[email protected]' => 'John Doe'))
                ->setTo('[email protected]')
                ->setSubject('some subject');

            $message->setBody($this->getPartial('global/mail_partial'));

            $this->getMailer()->send($message);

I had already tried to change the header Content-type of the email message using some specific Swift methods but it is not working.

Jahn answered 24/6, 2010 at 16:59 Comment(1)
Hi there! I had discovered that setting the message body as HTML also can by done passing a second argument on the $message->setBody() method, as example: $message->setBody($this->getPartial('global/mail_partial'), 'text/html');. Anyway, thanks for the help! Best regards!Yorgen
B
50

See:

Sending a HTML E-Mail (from SwiftMailer Docs)

You need to add this line to set html content-type:

$message->setContentType("text/html");

Alternatively, it can by done passing a second argument on the $message->setBody() method:

$message->setBody($this->getPartial('global/mail_partial'), 'text/html');.
Balmoral answered 24/6, 2010 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.