1 $filename = 'myfile';
2 $path = 'your path goes here';
3 $file = $path . "/" . $filename;
4
5 $mailto = 'mail@mail.com';
6 $subject = 'Subject';
7 $message = 'My message';
8
9 $content = file_get_contents($file);
10 $content = chunk_split(base64_encode($content));
11
12 // a random hash will be necessary to send mixed content
13 $separator = md5(time());
14
15 // carriage return type (RFC)
16 $eol = "\r\n";
17
18 // main header (multipart mandatory)
19 $headers = "From: name <test@test.com>" . $eol;
20 $headers .= "MIME-Version: 1.0" . $eol;
21 $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
22 $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
23 $headers .= "This is a MIME encoded message." . $eol;
24
25 // message
26 $body = "--" . $separator . $eol;
27 $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
28 $body .= "Content-Transfer-Encoding: 8bit" . $eol;
29 $body .= $message . $eol;
30
31 // attachment
32 $body .= "--" . $separator . $eol;
33 $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
34 $body .= "Content-Transfer-Encoding: base64" . $eol;
35 $body .= "Content-Disposition: attachment" . $eol;
36 $body .= $content . $eol;
37 $body .= "--" . $separator . "--";
38
39 //SEND Mail
40 if (mail($mailto, $subject, $body, $headers)) {
41 echo "mail send ... OK"; // or use booleans here
42 } else {
43 echo "mail send ... ERROR!";
44 print_r( error_get_last() );
45 }
46
1$my_file = "somefile.zip";
2$my_path = "/your_path/to_the_attachment/";
3$my_name = "Olaf Lederer";
4$my_mail = "my@mail.com";
5$my_replyto = "my_reply_to@mail.net";
6$my_subject = "This is a mail with attachment.";
7$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
8mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
1<?php
2function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
3 $file = $path.$filename;
4 $file_size = filesize($file);
5 $handle = fopen($file, "r");
6 $content = fread($handle, $file_size);
7 fclose($handle);
8 $content = chunk_split(base64_encode($content));
9 $uid = md5(uniqid(time()));
10 $header = "From: ".$from_name." <".$from_mail.">\r\n";
11 $header .= "Reply-To: ".$replyto."\r\n";
12 $header .= "MIME-Version: 1.0\r\n";
13 $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
14 $header .= "This is a multi-part message in MIME format.\r\n";
15 $header .= "--".$uid."\r\n";
16 $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
17 $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
18 $header .= $message."\r\n\r\n";
19 $header .= "--".$uid."\r\n";
20 $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
21 $header .= "Content-Transfer-Encoding: base64\r\n";
22 $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
23 $header .= $content."\r\n\r\n";
24 $header .= "--".$uid."--";
25 if (mail($mailto, $subject, "", $header)) {
26 echo "mail send ... OK"; // or use booleans here
27 } else {
28 echo "mail send ... ERROR!";
29 }
30}