6

I am just trying to get my multipart emails encoded with base64 and send via swiftmail. Here is the code I have so far:

$message = Swift_Message::newInstance("Email Template Test")
    ->setBoundary($boundary)
    ->setFrom(array('no-reply@domain.net' => 'Mailer Service'))
    ->setTo(array("a@d.com","a@b.com"))
    ->setBody($plaintext)
    ->addPart($htmlmail,"text/html");

$headers = $message->getHeaders();
$headers->addTextHeader('Content-Transfer-Encoding','base64');

$contenttype = $message->getHeaders()->get('Content-Type');
$contenttype->setValue('multipart/alternative');

As far as I can see from the documentation (which I don't find too clear), The Content-Transfer-Encoding header is a text header, so i should be able to set it as above. Before this, I ran an output of all the current headers, and Content-Transfer-Encoding was not listed in there, so It needed to be set. Hence why in the above code I have tried to set it.

The output is fine, I get the emails, they work, but when I view source they are not encoded. I have tried with the same above code but changing $plaintext to base64_encode($plaintext), but just received the encoded message. How is it done>

Chud37
  • 4,907
  • 13
  • 64
  • 116

2 Answers2

4

In version 5.4 you can set the encoder. Otherwise Swift_Message will use the native encoder to encode the message.

$message = \Swift_Message::newInstance("Email Template Test");
$message->setEncoder(\Swift_Encoding::getBase64Encoding());
//...

Additionally there is a bug (as of version 4 and 5) with encoding and addPart. Where the MimePart will not inherit the encoding from the origin message. To do this you need to manually create the MimePart and attach it to the origin message.

$part = \Swift_MimePart::newInstance();
$part->setEncoder($message->getEncoder());
$part->setBody($htmlmail, 'text/html');
$message->attach($part);

This will automatically add the Content-Type: multipart/alternative; boundary=****, boundary charset and Content-Transfer-Encoding: base64 header information as well.

Result:

var_dump($message->toString());

string 'Message-ID: <2f48c04910b97f730834e92f268d3410@example.com>
Date: Thu, 14 Jan 2016 20:45:30 +0000
Subject: Email Template Test
From: Mailer Service <no-reply@domain.net>
To: a@d.com, a@b.com
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_"


--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

VGhpcyBpcyBhbiBodG1sIG1lc3NhZ2U=

--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64

VGhpcyBpcyBhIHRleHQgbWVzc2FnZQ==

--_=_swift_v4_1452804330_b0a47ad936ba98d2f513294958a235d0_=_--
' (length=751)

SwiftMailer 6

In swiftmailer 6 the Swift_Encoding class and ::newInstance() methods were removed.

The original usage of \Swift_Encoding::getBase64Encoding(), looked like

public static function getBase64Encoding()
{
    return self::_lookup('mime.base64contentencoder');
}


private static function _lookup($key)
{
    return Swift_DependencyContainer::getInstance()->lookup($key);
}

Therefor you can call the mime.base64contentencoder directly from the Swift_DependencyContainer instead.

$encoder = \Swift_DependencyContainer::getInstance()->lookup('mime.base64contentencoder');

$message = (new \Swift_Message("Email Template Test"))
    ->setEncoder($encoder);
Will B.
  • 17,883
  • 4
  • 67
  • 69
0

I just wanted to do this myself recently, and

\Swift_Encoding::getBase64Encoding()

is apparently removed so I had to use Swift_DependencyContainer like this:

$message->setEncoder(\Swift_DependencyContainer::getInstance()->lookup('mime.base64contentencoder'));

Now swiftmailer will use base64 as the content transfer encoding.

MrTasken
  • 1
  • 1