Aid me with php imap? [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP imap problems
I need to be able to use my mail (gmail) from a php script. But wherever I try, the email body comes out all rubbbish with characters like =3D and random equals signs. Sometimes it comes in as base64 or nothing at all. How can I get an email and display it in HTML Purifier clean html or plain text in a pre tag if no html is available.?
$overview = imap_fetch_overview($inbox,$email_number,0);
$body_pre = imap_fetchbody($inbox, $email_number, 2.1);
$message = imap_fetchbody($inbox, $email_number, 2.2);
$message = base64_decode($message);
if (empty($message)){
$message = $body_pre;
$message = preg_replace('#(https?://([-\w\.]+)+(:\d+)?((/[\w/_\.%\-+~]*)?(\?\S+)?)?)#', '$1',$message);
$message = '<pre>'.htmlentities($message).'</pre>';
}else{
$cleanconfig = HTMLPurifier_Config::createDefault();
$cleanconfig->set('Core.Encoding', 'UTF-8');
$cleanconfig->set('HTML.Doctype', 'HTML 4.01 Transitional');
$purifier = new HTMLPurifier($cleanconfig);
$message = $purifier->purify($message);
}
this code, $message just comes blank.

The message with "characters like =3D and random equals signs" is quoted-printable encoded. You can decode using php with quoted_printable_decode.
There are different ways how mail messages can be encoded, like quited-printable or base64. The sender decides on the used encoding.

Related

PHP not encoding quoted_printable_encode correctly

I'm trying to add an emoticon(fire) in email subject. Below is the code I'm using:
$subject = "Test email \xF0\x9F\x94\xA5";
$charset = "UTF-8";
$subject = sprintf('=?%s?Q?%s?=', $charset, quoted_printable_encode($subject));
// send email....
This is adding a fire emoticon to the email subject successfully. But when I try to get the subject from database like:
$subject = $themeArray['templateSubject']; // Test email \xF0\x9F\x94\xA5
$charset = "UTF-8";
$subject = sprintf('=?%s?Q?%s?=', $charset, quoted_printable_encode($subject));
// send email....
This is not converting the characters to emoticon. Instead I'm getting the subject in email as
Test email \xF0\x9F\x94\xA5
Why its not converting when I get subject from database?
Can anyone help me to fix this? Thanks in advance.

php imap_fetchbody returns strange characters

Im trying to get the actual text message part of the email with:
$body = imap_fetchbody($inbox, $email_number, 1.2);
Problem is I get this:
WXuQFOW1/53v6+6Z2Zmdnd1Z9gW7vHYREHZZtzaA4IOAMQKJmBARKfNWkwjRingtklgatEwuPlG4
RI2liSZKAAvwFRKuIlwVUYw8RJ4iKwu7yz7nuTPd/Z37R8/Mzq6zat2qm1RtV1dRS3d/33d+53fO
+Z0zxMxIXwwGExEYzKwIUEAoEv3w+Nljp86FIjFDlyXBgknVw0dXBKWuESCIAHIWISIM6Uvr9xc7
9jIY4Wji9Xc+
Do I need to do any kind of encoding here?
I had the same problem and I solved by placing this code:
$message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,2));

How to get imap body text with german locale in php?

Facing the issue that when I try to get the body of an email through imap in php, the content of the variable $body is
Jääätzt Kööönmän umläute mit ÄnhÄÄÄng
instead of
Jääätzt Kööönmän umläute mit ÄnhÄÄÄng.
I do not understand where the problem is, all other characters like ?,!,$ are working fine, just german characters like ä,ö are making those problems.
The code-block in php I am using is
$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($mbox, $overview->msgno, 1);
}
Do you have any ideas? I am really desperate.
The Mail you try to show is not in the same encoding as the headers of your website. That's why your browser can't show the special chars properly, like the Umlaut in this case.
You can for example convert them to utf-8 with imap_utf8
$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($mbox, $overview->msgno, 1);
}
$body = imap_utf8($body);
http://php.net/manual/de/function.imap-utf8.php

How to get special characters of the body of an email using imap with PhP?

I'm using imap with php to get the body of my emails.
With what've coded so far, I am not getting the special characters and weird formatting at the end. For instance, if I send myself an email (with a regular email client such as Apple Mail) that says:
Test with characters é à and some ! that rock.
What I get with php is:
Test with characters =C3=A9 =C3=A0 and some ! that rock.=
I've tried re-sending the body through the mail function of php with those headers, but I'm still getting the same problem.
$headers="From: address#email.com" . "\r\n" . "MIME-Version: 1.0" . "\r\n" . "Content-type:text/html;charset=UTF-8";
mail($sendTo, $message, $noBody, $headers);
I've tried addslashes(), which didn't help either.
Sample of my code here:
$imapLink=imap_open("{".$mailbox."}INBOX",$username,$password);
$mailBoxInfos = imap_check($imapLink);
$mailList = imap_fetch_overview($imapLink,"1:".$mailBoxInfos->Nmsgs);
if(isset($mailList))
{
$numMess = imap_num_msg($imapLink);
while($numMess>0) {
$message = imap_body($imapLink, $numMess);
$numMess --;
}
}
$imapClose = imap_close($imapLink);
Hope you can help me with that!
Thanks in advance!
Arthur
The =XX you see in your text is called "Quoted-Printable Content". It's used to display character which can't be displayed in 7-bits.
You can use quoted_printable_decode() to convert it back to a readable string.
So this should work:
$message = imap_body($imapLink, $numMess);
$message = quoted_printable_decode($message); // <-- add this line
$numMess --;

PHP code isnt sending email [duplicate]

How to parse a .eml file in php? Is there any PHP libriary or PHP extension ?
I want to display the mail header information such as sender, receiver, title, attachement and eml body content in browser.
There are a couple of ways to do it. One way is to simply do it yourself, it's not that complicated.
Otherwise, you might want to have a look at the Mailparse library:
http://php.net/manual/en/book.mailparse.php
And there is also this one:
http://code.google.com/p/php-mime-mail-parser/
This is what I use:
composer require php-mime-mail-parser/php-mime-mail-parser
And then PHP:
$parser = new \PhpMimeMailParser\Parser();
$emailFile = 'myEmailFile.eml';
$parser->setText(file_get_contents($emailFile));
Then, to get addresses:
$toAddressesQ = $parser->getAddresses('to');
Or the body:
$text = $parser->getMessageBody('text');
$html = $parser->getMessageBody('html');
Or the header:
$subject = $parser->getHeader('subject');
Or the attachments:
$attachments = $parser->getAttachments();

Categories