Prepare email body for PHPMailer - php

I stuck in a problem using 1C-Bitrix framework with PHPMailer. The problem connected with the email body.
1C-Bitrix framework prepares body depending on options enabled. For example, if I turn on the option "Create text vesion of html-email" the body will look like this:
---------alt8045b59706
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This is my email
---------alt8045b59706
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<h1> This is my email </h1>
---------alt8045b59706--
If I the email template will have an attached txt file, the body will look like this:
---------mix4215b5973f
Content-Type: multipart/alternative; boundary="-------alt3795b5973f"
---------alt3795b5973f
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This is my email
---------alt3795b5973f
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<h1> This is my email </h1>
---------alt3795b5973f--
---------mix4215b5973f
Content-Type: text/plain; name="=?UTF-8?B?dGVzdC50eHQ=?="
Content-Transfer-Encoding: base64
Content-ID: <427605>
dGVzdCB0ZXh0Cg==
---------mix4215b5973f--
And, finally, the standard email body will look like this:
<h1> This is my email </h1>
I can't just feed such email body to PHPMailer. It doesn't work correctly. I tried msgHtml() - also doesn't work.
But standard php mail() func works well with such email body.
I found that in PHPMailer I need to set Body and AltBody separately. But then, what is the best way for me to parse such email body.
Maybe somebody knows a tool for it?
Or PHPMailer has a built in methods for it?
Maybe I need to just manually set correct "Content-Type" header (framework tells me correct Content-Type)
Thanks, in advance.

You don't need to think about those things - PHPMailer does it for you. This is all you need to do to make that structure:
$mail->isHTML();
$mail->Body = '<h1> This is my email </h1>';
$mail->AltBody = 'This is my email';
$mail->addAttachment('path/to/file.pdf');
Also, the MIME structure in the example you gave for the message body with an attachment is incorrect, so I wouldn't expect it to work in any context.

Related

Gmail displaying plain text instead of HTML

Situation
I have a script that is downloading emails from a Gmail account, tweaking the content, and re-sending out the emails.
Problem
Whenever I create an email with an embedded image, Gmail displays the plain text version of the email and the embedded image appears like a regular attachment.
Tested
If I use a different client, the image displays properly. If I forward the email from Gmail to a different client, the image displays properly. Multi-part emails that have regular attachments, or that have both plain text and html parts display properly - as long as they don't have an embedded image.
Email format
From what I can see, the format of the email is correct, but I've pasted it below, trimmed for brevity & privacy. The 2 things I notice is that the boundary appears to be properly set, and the img src cid: matches the Content-ID of the image.
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_"
--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
This email has been sent to a Mailing List. You can approve or reject it a=
t [URL here]
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
[image: noice.gif]
-----------------------
[Signature here]
--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<p>
This email has been sent to a Mailing List. You can approve or reject =
it at [URL here]</p>
<hr />
<div dir=3D"ltr"><div><img src=3D"cid:7f82a31e4f084e8f0a25edd913ed3aa2#swif=
t.generated" alt=3D"noice.gif" width=3D"474" height=3D"244"><br></div><div>=
<div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signatur=
e"><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><br><br>---=
--------------------<br>[signature here]<br></div>=
</div></div></div></div></div></div></div>
--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_
Content-Type: image/gif; name=noice.gif
Content-Transfer-Encoding: base64
Content-ID: <7f82a31e4f084e8f0a25edd913ed3aa2#swift.generated>
Content-Disposition: inline; filename=noice.gif
R0lGODlh2gH0APcAAAgDBRGJG5ZKGsDGB8lNFJJsGkkIBs+nLgnCM4lPZODnDqdMGo4rCVGIKs5q
[... bunch of base64 encoded stuff]
BFGgAUsAEvjkAlQgq/drOjfkFKdEETNBxl3bpi5lBYEAADs=
--_=_swift_1575407655_756d311b6808cc9e07d75ccca0ed9b6c_=_--
PHP code
Here is the PHP code that generates the Mailer object - which is a wrapper around Swiftmailer, and is used to do the actual sending:
$Mailer = new Mailer($subject_prepend.$this->subject);
// Simply a wrapper. Calls $this->Message->setBody($body,'text/plain');,
// where $this->Message is an instance of Swift_Message
$Mailer->setBody($plaintext_prepend.$this->plaintext);
$html_body = $html_prepend.$this->html;
if(count($this->Attachments)){
foreach($this->Attachments as $Attachment){
if($Attachment->isEmbedded()){
$image = \Swift_Image::fromPath($Attachment->getPath())->setDisposition('inline');
$cid = $Mailer->Message->embed($image);
//$cid = $Mailer->embedPath($Attachment->getPath());
$html_body = str_replace('cid:'.$Attachment->cid, $cid, $html_body);
}
else{
// calls $this->Message->attach(\Swift_Attachment::fromPath($path));
$Mailer->addAttachmentPath($Attachment->getPath());
}
}
}
// calls $this->Message->addPart($body,'text/html');
$Mailer->addHTMLBody($html_body);
The commented out line is how I was originally embedding the image, but it had the same result.
-
Is this some Gmail magic that's failing me, or is the email misconfigured somehow?
Edit
I managed to get the email sending properly with PHPMailer. The only difference I can see is that PHPMailer starts with:
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="b1_6hukH7nTGJu6fpr5tpXob5uQE7wXivW0oMppPNbwOi4"
Content-Transfer-Encoding: 8bit
where Swiftmailer starts with
MIME-Version: 1.0
Content-Type: multipart/related; boundary="_=_swift_1575416351_99d22ee774049152f712bc5ae65340fb_=_"
ie: PHPMailer uses multipart/alternative rather than multipart/related, and sets Content-Transfer-Encoding.
Also, all textual parts of SwiftMailer's email was `Content-Transfer-Encoding: quoted-printable' whereas PHPMailer didn't set that header.

unable to send email as both plain text and html in cakephp

App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->to($emailContentArray['To']);
$Email->from(array($emailContentArray['From'] => Configure::read('FROM_NAME')));
$Email->subject($emailContentArray['Subject']);
$Email->emailFormat('both');
$response=$Email->send($emailContentArray['Body']);
if we check the resulting email it looks like:-
Content-Type: multipart/mixed; boundary="782f009f669cbcf2faafff59fe0eeb5d"
Content-Transfer-Encoding: 8bit
X-Identified-User: {:test25.xyzzzz.com:testttt.com} {sentby:program running on server}
--782f009f669cbcf2faafff59fe0eeb5d
Content-Type: multipart/alternative; boundary="alt-782f009f669cbcf2faafff59fe0eeb5d"
--alt-782f009f669cbcf2faafff59fe0eeb5d
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
<div>​<BR>Hi<BR><BR>Soon you will reach the action limit. To keep yourself updated please pay the payment.<BR><BR> Happy to have you<BR><BR>Thanks,<BR>Apps Team<BR>​</div>
--alt-782f009f669cbcf2faafff59fe0eeb5d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<div>​<BR>Hi<BR><BR>Soon you will reach the action limit. To keep yourself updated please pay the payment.<BR><BR> Happy to have you<BR><BR>Thanks,<BR>Apps Team<BR>​</div>
In text/plain the email should not show the html tags - how can I get both html and plain text to be sent correctly?
Encode the content and the subject in base64:
"=?utf-8?b?".base64_encode($s)."?=";
And before sending the emails don't forget to set the header:
'Content-Transfer-Encoding: base64';
If I'm not wrong you can make to set header: $Email->setHeader('X-Content-Transfer-Encoding', 'base64');
You can take a look at Cakephp sending UTF-8 Emails and lineLength
And at http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

Contet type in PHP Mail

I have write a code to send a mail, the mail has send but the content of the mail is send as HTML coding, can anybody help to solve this problem.
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This is the content type I have used.
Thankyou
If your problem is to send email as plain text, than Content-Type must be changed to text/plain instead of text/html

PHP mail() specific question

So, I would like to use mail() to send registration emails for my website, however I'd like to make it look nice while falling back to good old plaintext when necessary; a mixed message email.
However I would like it to be sent from John Doe who's email is johndoe#example.com to recipient#example.com.
The HTML code should be <html><head><title>HTML email!</title></head><body><p>This is HTML!</p></body</html> and the plaintext message should be This is plaintext.
What would be the arguments to mail() to accomplish this? I know a lot of it deals with changing the header in some crazy way.
Thanks so much!
Use something like SwiftMailer instead, as it has nice things like header injection prevention. With that in mind, yes, you have to set custom headers and use a multi-part body to achieve what you want:
/***************************************************************
Creating Email: Headers, BODY
1- HTML Email WIthout Attachment!! <<-------- H T M L ---------
***************************************************************/
#---->Headers Part
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
AKAM;
#---->BODY Part
$Body =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="$boundary1"
This is a multi-part message in MIME format.
--$boundary1
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary1
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary1--
AKAM;
Source: http://www.php.net/manual/en/function.mail.php#83491
This is a lot of work. Which, once again, is why I recommend having a library that can handle all of this for you, plus other features.
Everything is here (Example #4): http://php.net/manual/en/function.mail.php

My multipart email script sends HTML messages just fine, but the plain text alternative doesn't not work, what may be wrong?

I have a script set up to send out multipart emails; plain text and html messages. The HTML messages work just fine, but when I used an email client that only does plain text the plaint text message does not render and I get the following:
--
This message was generated automatically by Me
http://www.somewebsite.com/
$html_msg = $message_details;
$plain_text_msg = strip_tags($message_details);
$headers = <<<HEADERS
From: Me <info#somewebsite.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="==PHP-alt$mime_boundary"
HEADERS;
// Use our boundary string to create plain text and HTML versions
$message = <<<MESSAGE
--==PHP-alt$mime_boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
$plain_text_msg
--
This message was generated automatically by Me
http://www.somewebsite.com/
If you did not request this message, please notify promotions#mewstavern.com
--==PHP-alt$mime_boundary
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>
<body>
$html_msg
<p>
--<br />
This message was generated automatically as a demonstration on
Me
</p>
<p>
If you did not request this message, please notify
info#somewebsite.com
</p>
</body>
</html>
--==PHP-alt$mime_boundary--
MESSAGE;
The issue was white space in the heredoc syntax I'm using. It is not represented in the above example.
#qor72 thank you for your input.

Categories