Zend Mail Imap: Fetch Body of Multipart? - php

I have a small "problem" with Zend_Mail_Storage_Imap and MultiPart Mails.
ContentType: multipart/alternative;
boundary=f46d043bd88a9f5d9004c87d2ad3
Part 1 has the Text of the Message, but with headers inside the content, so when i try
$part->getContent();
--f46d043bd88a9f5d9004c87d2ad3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hallo
is the result, how to extract the header information now?

Instead of:
$part->getContent();
do:
$part->getHeader();
to get Headers or if you want ONLY content then:
try:
while ($part->isMultipart()) {
$part = $message->getPart(1);
}
or
$message = $mail->getMessage($messageNum);
for content without body.
If issue persists, check your mail on mail client's like Gmail. Sometimes, we add header information twice (accidentally) whilst sending the mail.
Hope this helps :)

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.

Prepare email body for PHPMailer

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.

imap_fetchbody can't handle mails sent with phpmailer?

I am sending some emails with phpmailer with the regular example on their website.
the email is sent, and it arrives perfectly to the sent address.
after the mail is sent, I am saving the email to my sent items folder with imap_append, using the info of this answer Sent mails with phpmailer don't go to "Sent" IMAP folder
and in third place, I am creating a custom made webmail which reads my imap folders.
the problem is that I can't get the html body of the email sent correctly.
When I open the mail on sent items on thunderbird, or my phone, the body is shown perfectly.
but when I try to get the html body with php_imap functions I can't make it!!.
I created an isolated file to fetch the body of the msg like this:
$body = imap_fetchbody($conn,3392,1,FT_UID);
that returns this:
--b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ REMOVED CONTENT --------------------- klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit á é í ñ Ñ
REMOVED CONTENT
---------------------
klfasjdkl adj skldj klfdj akfdsj lkfdasjfkl dasjf --b1_9db4e4310d1b141cbec79dd7de22f70b--
If I try
$body = imap_fetchbody($conn,3392,1.2,FT_UID);
don't get anything
If I try
$body = imap_fetchbody($conn,3392,2,FT_UID);
don't get anything, it seems that PHPMailer creating the emailbody in a way that imap_fetchbody can't handle?
thank you

why i receive the body content mail duplicated when i use php://stdin?

I have a postfix server redirecting all incomming mail into a php script
but the body contents of the mail appears duplicated, looks:
--000e0ce03cf6d06b0c04c67595d4
Content-Type: text/plain; charset=ISO-8859-1
why
show
this
duplicated?
--000e0ce03cf6d06b0c04c67595d4
Content-Type: text/html; charset=ISO-8859-1
why<div>show</div><div>this</div><div>duplicated?</div>
--000e0ce03cf6d06b0c04c67595d4--
i have sended this mail using gmail with this body content:
why
show
this
duplicated?
why appears the mail two times? (and one in html :S)
how can i get only one?
thanks!
Assuming your code returns the same sort of separator, one method might be to do the following:
// Get separator
$mail_pieces = explode("\n", $mail);
$separator = $mail_pieces[0];
// Separate email
$mail_pieces = explode($separator, $mail);
$text_mail = $mail[0];
$html_mail = $mail[1];

PHP send MIME emails...doesn't work anymore

I just switched from a shared server to a virtual dedicated. The whole site works, but my mail forms have quit sending MIME emails the way they used to.
What used to appear before the server switch:
hallo fwend
What appears now in the emails (appears as though my client doesn't support MIME):
Content-Type: multipart/alternative; boundary="==DL-
bound_b08bcfabdc459ae7e87e493a984115b3"
This is a multipart MIME message from
your friends at Example.com. If you
are reading this, it seems we have a
message to deliver, but your mail
client is not set to receive these
types of messages. We are sorry for
the inconvenience :(
--==DL-bound_b08bcfabdc459ae7e87e493a984115b3
Content-Type: text/plain;
charset=iso-8859-1
Content-Transfer-Encoding: 7bit
hallo fwend
Is there some switch in PHP or something that I need turned on to send MIME?
It look like the headers are send in the body part of the message.
I had this problem a while back.
Possible causes are:
1. EOL
I used "\r\n" as eol for the MIME headers (following the spec)
, but the (shared)server wanted an "\n"
2. Last header
Some mail server add some headers of their own. If the last header end with "\n" and the mail server could also mess thing up.
3. Empty header
An empty header which causes a "\n\n" in the $headers will corrupt the headers. The "\n\n" aka "empty line" marks the end of the headers part of a email (or the start of the body)

Categories