Get the text of multipart Gmail messages - php

I am trying to fetch emails from Gmail by PHP using imap. I'm getting emails perfectly but my problem is that some of the messages are in a multipart format, and I would like to remove the multipart format.
For example, one Gmail message is "Hi, how are you? I am fine", but the message has text like this:
"Hi, how are you? I am fine
------=_NextPart_000_000B_01CE5613.4A5D9F20 // multipart format
Content-Type: text/html; // multipart format
charset="us-ascii" // multipart format
Content-Transfer-Encoding: quoted-printable". // multipart format

Hi Try this,
$mailNumber = '11796';
$result = $mailbox->getMessageContent($mailNumber);
if ($result->textHtml)
echo '<br>'.$result->textHtml;
else
echo '<br>'.$result->textPlain;

Can you try
imap_fetchbody($imap,$result,"1.2");
to
imap_fetchbody($imap,$result,"1.1");
1.2 is TEXT/HTML email part, which is used for html email body.
1.1 is TEXT/PLAIN - plain text email body - for plain text messages you'll need to use this one.

Related

How to do newline email body in laravel?

I want to send some information on mail body. When I try its works but Google shows trimmed content(...).
My code is like this:
$name = $e->name;
$code=$request->code;
$mes2="loretmtext,";
$mes3="loremtext";
$mes4="Code:".$code;
$mes5="e:".$name;
$mes6="text"
//and i send mail function
$msg= $mes2."<br />".$mes3."<br />".$mes4."<br />".$mes5."<br />".$mes6."<br />";
How can i solve?
Need to add "Content-Type: text/html; in header mail request to use html tag <br /> in you massage
look this:
Send HTML in email via PHP

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.

Incorrect Email(without html) is sending through Mailgun

I am using Mailgun API based extension in Expression engine CMS When i disable the Mail-gun its sending perfect html mail but when I enable Mailgun its sending Email like in this manner:
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_565e9f7740811
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
HELLO this is a message
--B_ALT_565e9f7740811
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<b>HELLO</b> this is a message
--B_ALT_565e9f7740811--
I type only once "HELLO this is a message" in this manner:
<b>HELLO</b> this is a message
But its showing two times in the email please resolve this issue.
I had so much headache with sending though an API I ended up buying Escort (https://devot-ee.com/add-ons/escort) and it simply worked perfectly and saved a lot of time making it worth it. I use it with Mandrill, but it supports yours.
Hope this kind of answer is allowed, as I know it isn't directly solving your problem.

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

Zend Mail Imap: Fetch Body of Multipart?

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 :)

Categories