I have a strange problem.
I am using PEAR SMTP on Ubuntu. When I send this message:
--==Alternative_Boundary_x{da5ccf044317613d9f8e371455f292a6}x
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
hello world - plain text test
--==Alternative_Boundary_x{da5ccf044317613d9f8e371455f292a6}x
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 7bit
<i>hello world - html test</i>
--==Alternative_Boundary_x{da5ccf044317613d9f8e371455f292a6}x--
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x
Content-Type: application/rtf; name="attachment.rtf"
Content-Disposition: attachment; filename="attachment.rtf"
Content-Transfer-Encoding: base64
e1xydGYxXGFuc2lcZGVmZjB7XGZvbnR0Ymx7XGYwXGZtb2Rlcm5cZnBycTFcZmNoYXJzZXQwIENv
dXJpZXIgTmV3O317XGYxXGZuaWxcZmNoYXJzZXQwIENvdXJpZXIgTmV3O319DQp7XGNvbG9ydGJs
IDtccmVkMFxncmVlbjBcYmx1ZTA7fQ0Ke1wqXGdlbmVyYXRvciBNc2Z0ZWRpdCA1LjQxLjE1LjE1
MTU7fVx2aWV3a2luZDRcdWMxXHBhcmRcY2YxXGxhbmcyMDU3XGYwXGZzMzIgSGVsbG8gd29ybGRc
Y2YwXGYxXGZzMjBccGFyDQpcY2YxXGlcZjBcZnMyNCBIZWxsbyB3b3JsZFxjZjBcaTBcZjFcZnMy
MFxwYXINClxiIEhlbGxvIHdvcmxkXHBhcg0KXGIwXHBhcg0KXHBhcg0KfQ0KAA==
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x
Content-Type: text/xml; name="attachment.xml"
Content-Disposition: attachment; filename="attachment.xml"
Content-Transfer-Encoding: base64
PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8YXR0YWNoZW1udD5pbiAueG1sIGZpbGU8L2F0dGFjaGVt
bnQ+IA==
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x--
I receive it without one of the --==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x (look where there are two in a row) missing. Some how PEAR smtp send sends the message with that missing.
But if I do it from Windows, using phplib, it sends just fine and causes the attachments to work!
I know I could use the PEAR Mime mail to deal with this, but unfortunately, I am working with legacy code and that is not an option since the message has already had all the encodings.
My question is, why does PEAR remove it? And what does it break the attachments when not there?
Thanks
I can only assume from what you give, but the Ascii-Art in your boundary is really nice, but just probably a little over the top. It contains characters that are not allowed there per the specs, and that are the curly brackets. Maybe because of having an invalid email there, things get dropped?
Got it working! Turns out the extra line was not supposed to be there at all and was causing all sorts of problems.
Managed to change some code so that it was not there and it all started working!
Related
I have written php code for reading email. I am using
imap_fetchbody($this->conn, $i, 1.2);
for displaying email message. When mail is with attachment it is display in well mannered but if email is without attachment it displays nothing. When I am using
$body = imap_fetchbody($this->conn, $i, 1);
message is displaying in text/plain format. like this
this is system generated mail -- Regards! Md Belal
When I am using
$body = imap_body($this->conn, $i);
it is displaying like this
--047d7b5d2f88ddb36c050069a175 Content-Type: text/plain; charset=UTF-8 this is system generated mail -- Regards! Md Belal 8287326270 --047d7b5d2f88ddb36c050069a175 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
this is system generated mail
--
Regards!
Md Belal
--047d7b5d2f88ddb36c050069a175--
I need to display it in well mannered. Like this.
this is system generated mail
--
Regards!
Md Belal
What to do please help. Thanks!
Try this
https://stackoverflow.com/a/14936048/2289510
btw i haven't tried but nl2br (http://hu1.php.net/nl2br) may can help you too, its adding line breaks when your result is in text/plain format and its contains \n \r characters.
PHP -IMAP
I have saved all imap mail to database and displaying them from database. My problem is that one mail is showing "Content-Transfer-Encoding: base64". If i base64 decode it then other mails also get affected. I haven't been able to change it to human readable format till yet. Any help will be highly appreciated.
I have tried many solution but The code currently I am using is,
mb_convert_encoding($body, "ISO-8859-1", "UTF-8");
The output of this mail is
------=_NextPart_001_0606_012497BF.12408D60
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: base64
OTA5MTU5Mzg3DQoNCuS7juaKgOacr+i1sOWQkeeuoeeQhueahOWbm+S4quaguOW/gw0KMjAxMi05
LTE1ICAxNjowMjoxNw==
------=_NextPart_001_0606_012497BF.12408D60
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: base64
PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIj4NCjxIVE1MPjxIRUFEPg0KPE1FVEEgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXV0
Zi04IiBodHRwLWVxdWl2PUNvbnRlbnQtVHlwZT4NCjxNRVRBIG5hbWU9R0VORVJBVE9SIGNvbnRl
bnQ9Ik1TSFRNTCA4LjAwLjYwMDEuMTg5OTkiPjwvSEVBRD4NCjxCT0RZPg0KPFA+PEZPTlQgc2l6
ZT0yPjkwOTE1OTM4NzxCUj48QlI+5LuO5oqA5pyv6LWw5ZCR566h55CG55qE5Zub5Liq5qC45b+D
PC9GT05UPjwvUD4NCjxQPjxGT05UIHNpemU9Mj4yMDEyLTktMTUmbmJzcDsgMTY6MDI6MTc8L0ZP
TlQ+PC9QPjwvQk9EWT48L0hUTUw+DQo=
------=_NextPart_001_0606_012497BF.12408D60--
It's base64, you have to use base64_decode on body part like this:
base64_decode('OTA5MTU5Mzg3DQoNCuS7juaKgOacr+i1sOWQkeeuoeeQhueahOWbm+S4quaguOW/gw0KMjAxMi05LTE1ICAxNjowMjoxNw==');
just to manually check there are several online services like
http://www.motobit.com/util/base64-decoder-encoder.asp
just copy and paste the base64 source and choose "decode". You will get a "human readyble format".
I'm trying to generate EML files from PHP. Is there any library that will allow me to easily create them? I could find some ActiveX component on the internet but would rather use something more portable.
I ended up building the MIME message myself using this kind of template, where each field is replaced by a TEMPLATE_<name> variable:
From: TEMPLATE_FROM_ADDRESS
MIME-Version: 1.0
To: TEMPLATE_TO_ADDRESS
Subject: TEMPLATE_SUBJECT
Content-Type: multipart/mixed; boundary="080107000800000609090108"
This is a message with multiple parts in MIME format.
--080107000800000609090108
Content-Type: text/plain
TEMPLATE_BODY
--080107000800000609090108
Content-Type: application/octet-stream;name="TEMPLATE_ATTACH_FILENAME"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="TEMPLATE_ATTACH_FILENAME"
TEMPLATE_ATTACH_CONTENT
--080107000800000609090108
Then creating the final message is quite simple using str_replace:
$content = file_get_contents("Template.eml");
$content = str_replace("TEMPLATE_FROM_ADDRESS", $fromEmail, $content);
$content = str_replace("TEMPLATE_TO_ADDRESS", $toEmail, $content);
// etc. for each template parameter
// Also don't forget to base64_encode the attachment content;
$content = str_replace("TEMPLATE_ATTACH_CONTENT", base64_encode($attachContent), $content);
Additional info about file attachment in this post: Attachment name and file extension not working in email *.eml
Edit (2018): Since this answer was written it seems it's been copied and pasted a bit everywhere, the template in particular. To avoid any conflict with other MIME data, you should make sure that the boundary "080107000800000609090108" is unique - it's a string of random characters no longer than 70 characters.
I think you don't need a library. It's just plain text (e.g. http://bitdaddys.com/example1.eml)
Date: Sat, 12 Aug 2006 14:25:25 -0400
From: John Doe <jdoes#someserver.com>
Subject: BitDaddys Software
To: sales#bitdaddys.com
Dear BitDaddys Corp.,
We have added your software to our approved list.
Thank you for your efforts.
Sincerely,
John Doe
Some Server Company
You can just output text with headers and save it using fwrite. For attachments use base64_encode() as stated here
Use imap_savebody (part of the imap library http://us1.php.net/manual/en/function.imap-savebody.php) with a null $part_number. It creates a beautiful .eml file with one line of code with the entire message (null $part_number = all parts... not documented but works).
the other two solutions depend on the format of the email (only one attachment and no html section in the first solution, and only text email in the second).
imap_savebody creates a perfect .eml file no matter what the format of the incoming email is (as long as it's RFC-complaint of course).
Hey Guys,
I am totally confused on PHP's imap functions, they are all over the place and I've dug through comments to get this far. I can do imap_fetchbody($connection, 5, null); and retrieve the entire message which gives me all that i need:
ie:
To: user#gmail.com Content-Type:
multipart/mixed;
boundary=001517570c8a681a5004a36a1fd6
--001517570c8a681a5004a36a1fd6 Content-Type: multipart/alternative;
boundary=001517570c8a681a4804a36a1fd4
--001517570c8a681a4804a36a1fd4 Content-Type: text/plain;
charset=ISO-8859-1
asdf
--001517570c8a681a4804a36a1fd4 Content-Type: text/html;
charset=ISO-8859-1
asdf
--001517570c8a681a4804a36a1fd4--
--001517570c8a681a5004a36a1fd6 Content-Type: image/gif;
name="ajax-loader.gif"
Content-Disposition: attachment;
filename="ajax-loader.gif"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gnruant80
... bunch of code here omitted ...
But I really want this information in an Array or Object to use it. without having to think up some regular expression to get data out of the entire texts above.
Does anyone know a way to do that or which php imap command to use? Or is there a place with really good examples anyone knows of?
You can use a non null value for section parameter(3rd parameter). Please see second example of http://php.net/manual/en/function.imap-fetchbody.php. I think that is what you are searching.
The code below has been sending CSVs to our print fulfillment folks for years. Beginning of the week the sysadmin switched from sendmail to qmail for reasons having to do with procmail recipes we want to run.
Probably not coincidentally we starting started hearing that the fulfillment folks were seeing empty CSVs even when others CCed on the mail saw the records. Folks with the issue were seeing the attachment and could open it but their MUI was listing it as 131 bytes or zero bytes.
We started sending to a Yahoo address with same results. However Gmail sees attachment with correct rows. Mind you this is all one CCed email with different results depending on mail clients.
I have gone over the code in vi and made sure there were no ^M chars or other control character junk.
Anyone seen this before? Any suggestions most welcome!
Thanks!
$message = "Here is the file (comma-separated values) of addresses for\n";
$message .= $pm_row['title'] . " Requests ($now_YmdHMS).\n\n";
$data_email = $pm_row['fulfillment_data_email'];
$data_email_cc = "$pm_row[fulfillment_data_email_cc],$developer_email";
$subject = $pm_row['title'] . " Requests ($now_YmdHMS)";
$random_hash = md5(date('r', time()));
$headers = "From: XXX <tourism#xxx.org>\r\nReply-To: tourism#xxx.org\r\nCc:$data_email_cc";
$headers .= "\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
$output = "
--PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
--PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
$message
--PHP-alt-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
$message
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/zip; name=$now_YmdHMS.$pm_row[handle].csv
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
mail($data_email, $subject, $output, $headers);
I think it is a CR/LF problem, which is a known Bug in php for about three years and -as far as I know- hasn't been fixed up to now:
http://bugs.php.net/bug.php?id=15841
The generated Email isn't valid (explanation can be found here: http://cr.yp.to/docs/smtplf.html ), due to using a non-RFC-conform linebreak-format. Other MTA like sendmail and postfix correct this problem automatically; qmail doesn't.
You can either: write correct mails with php (lol), or ask your qmail-administrator to use the QmailScanner ( http://qmail-scanner.sourceforge.net/ ), which is doing this job too.
The best solution would be to deinstall php and use perl in the future duck ;)