[TYPO3-dev] t3lib_htmlmail breaks plaintext version when inline images are attached
Simon Tuck
stu at rtpartner.ch
Thu Aug 13 11:19:00 CEST 2009
Hi,
I'm using t3lib_htmlmail to send html emails with a plaintext
alternative and inline images. Unsuccessfully so far. So long as the
email doesn't contain any images the plaintext version of the mail is
available as an alternative in my mail client. However, as soon as the
message has one or more inline images the plaintext version of the
message is no longer available.
As far as I can tell (from googling the issue and experimenting with the
raw source of the email) the structure of the multipart mail is wrong.
The structure I get from t3lib_htmlmail is:
[SNIPPET]
Content-Type: multipart/related;
boundary="----------boundary_related"
----------boundary_related
Content-Type: multipart/alternative;
boundary="----------boundary_alternative"
----------boundary_alternative
Content-Type: text/plain;
charset=utf-8
----------boundary_alternative
Content-Type: text/html;
charset=utf-8
----------boundary_alternative--
----------boundary_related
Content-Id: <xyz.1>
Content-Type: image/gif
----------boundary_related
Content-Id: <xyz.2>
Content-Type: image/jpeg
----------boundary_related--
[/SNIPPET]
The multipart/alternative which contains the html & plaintext versions
of the mail is enclosed in a multipart/related which attaches the inline
images. Using this source my mail client renders the html version ofthe
mail properly but fails to detect the plaintext version of the email.
Some experimentation led me to the following message structure, which
works as desired:
[SNIPPET]
Content-Type: multipart/alternative;
boundary="----------boundary_alternative"
----------boundary_alternative
Content-Type: text/plain;
charset=utf-8
------------boundary_alternative
Content-Type: multipart/related;
boundary="----------boundary_related"
----------boundary_related
Content-Type: text/html;
charset=utf-8
----------boundary_related
Content-Id: <xyz.2>
Content-Type: image/gif
----------boundary_related
Content-Id: <xyz.1>
Content-Type: image/gif
----------boundary_related--
----------boundary_alternative--
[/SNIPPET]
Here the multipart/alternative encloses the multipart/related which
attaches the inline images to the html version only.
I'm no expert on multipart message formats, does anyone have anymore
insight into this issue: Is this a bug in t3lib_htmlmail or am I using
the t3lib_htmlmail class wrongly (or is my mail client wrong - I'm using
apple mail btw)?
The code I am running to create and send an email is roughly as follows:
[SNIPPET]
// Get an instance of the TYPO3 mailer class
$this->mailer = t3lib_div::makeInstance('t3lib_htmlmail');
$this->mailer->start();
// Set the encoding to base64
$this->mailer->useBase64();
// Get/set the recipient(s)
if(!is_array($recipients)) $recipients = t3lib_div::trimExplode(',',
$recipients, 1);
$this->mailer->setRecipient($recipients);
// Subject
$this->mailer->subject = $subject;
// Sender information
$this->mailer->from_email = $fromEMail;
$this->mailer->from_name = $fromName;
$this->mailer->organisation = $organisation ? $organisation : '';
// Return path
$this->mailer->replyto_email = $fromEMail;
$this->mailer->replyto_name = $fromName;
$this->mailer->returnPath = $fromEMail;
// Xid
$this->mailer->Xid = md5(uniqid(mt_rand(), true));
// Add the plaintext version
$this->mailer->addPlain($text);
// Get the HTML version from the file
$this->addHtml($file);
// Add attachments
if(!empty($attachments)) {
foreach ($attachments as $attachment) {
$attachment = t3lib_div::getFileAbsFileName($attachment, false);
if(@is_readable($attachment)) {
if($attachment) $this->mailer->addAttachment($attachment);
}
}
}
// Send the mail
$this->mailer->setHeaders();
$this->mailer->setContent();
$mailWasSent = $this->mailer->sendTheMail();
return $mailWasSent;
[/SNIPPET]
Thanks for your advice.
Best,
Simon
More information about the TYPO3-dev
mailing list