PHP mail, MIME

the mail() function

Moderators: macek, egami, gesf

andynic
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Feb 29, 2012 4:33 am

PHP mail, MIME

Postby andynic » Sun Mar 03, 2013 1:32 pm

Hi,

I have been trying to use the php mail function to send an email with an attachment. I can get it to work when the attachment is a text file, but not when it is a file made in MS Word. Ideally I would like to be able to send any kind of attachment but lets start with a ".doc" file.

Please see the end of this file between the "tags" <CODE> .... </CODE> for the script that I have written. It was originally based on code borrowed from:
http://www.finalwebsites.com/forums/top ... ent-script.
That gave me problems similar to the ones described below so after reading the tutorial at:
http://www.tutorialized.com/tutorial/Se ... in-PHP/653
I modified it a bit. That is the code shown below.

Below that is the the contents of the first few lines of the received attachment (gibberish) viewed using MS Word 2003. The attachment was made using MS Word 2003 and named x1.doc. It shows up in the email as ATT00051.txt rather than x1.doc (one problem, but not the major one).

I have uploaded one attachment:
emailReceived.png -- a screen-shot of the resulting e-mail

I would be very grateful for any help you can give to help me get this to work properly.
Andynic
<CODE>
<?php
$my_file = "x1.doc";
$my_path = "/Users/andynic/Sites/tobivroegh.dev_v6.0/public_html/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipientsAddress@serviceProvider.com", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);


function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));

$uid = md5(uniqid(time()));
$name = basename($file);

$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";

$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\";\r\n\r\n";

$header .= "Content-Transfer-Encoding: 7 bit"; // added

$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";

$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";

$header .= "Content-Type: application/octet-stream;";
$header .= "name=\"$filename\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Description: \"word document\"\r\n";
$header .= $content;
$header .= "--".$uid."--";

$f = "-f $mailto"; //>>>>>>>Added this line; without the -f parameter the server did not send the mail
if (mail($mailto, $subject, "x.doc", $header, $f)) { //>>>>>>>Added $f because " " ....
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
?>
</CODE>
========
The first 15 lines of the attachment received which contains about 13 similar pages viewed with MS Word 2003:
========
Content-Type: application/octet-stream;name="x1.doc"
Content-Transfer-Encoding: base64
Content-Description: "word document"
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAASAAAAAAAAAAA
EAAASgAAAAEAAAD+////AAAAAEcAAAD/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////s
pcEAI2AJBAAA8BK/AAAAAAAAEAAAAAAABgAAWwgAAA4AYmpiam2lbaUAAAAAAAAAAAAAAAAAAAAA
AAAJBBYALhIAAA/PAAAPzwAAGQAAAAAAAAA8AAAAAAAAAAAAAAAAAAAABQAAAAAAAAD//w8AAAAA
AAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAAKQAAAAAAJQKAAAAAAAAlAoAAJQK
Attachments
emailReceived.png
emailReceived.png (66.54 KiB) Viewed 493 times

andynic
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Feb 29, 2012 4:33 am

Re: PHP mail, MIME

Postby andynic » Mon Mar 04, 2013 4:23 am

I found what (for my situation) is a simple solution that worked the first time around that I tried it.
It is at http://designbyelle.com.au/journal/2008 ... ttachment/
Then click on the link "PHP email attachment form" in the paragraph that begins:
In any case, here is my version of the PHP email attachment form.
Elle means what she says " Simple to implement in your web page."
andynic


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest