robert_gsfame Posted November 2, 2009 Share Posted November 2, 2009 guys, i wish that anyone can help me solving this problem I actually want to let user send email with attachment to other users (Yahoo or google email) Let say user has uploaded a word document, and the name of the document is XXX.txt and it has been stored into mysql database How can i send this together with an email to users (Yahoo or google email) using mail() thanx in advance Link to comment https://forums.phpfreaks.com/topic/179968-mail-with-attachment/ Share on other sites More sharing options...
abazoskib Posted November 2, 2009 Share Posted November 2, 2009 Here's the function I've been using for a while now to send email with attachements. To get a user selected file, you'll have to add an upload field to your form, and work out the details. This should get you started though. <?php function mail_attachment($file, $mailto, $from_mail, $from_name, $replyto, $subject, $body) { $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); $headers = "From: $from_name<" . $from_mail . ">\n"; $headers .= "Reply-To: <" . $from_mail . ">\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n"; $headers .= "X-Sender: $from_name<" . $from_mail . ">\n"; $headers .= "X-Priority: 1\n"; //1 = Urgent, 3 = Normal $headers .= "Return-Path: <" . $from_mail . ">\n"; $headers .= "This is a multi-part message in MIME format.\n"; $headers .= "------=MIME_BOUNDRY_main_message \n"; $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n"; $message = "------=MIME_BOUNDRY_message_parts\n"; $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; $message .= "Content-Transfer-Encoding: quoted-printable\n"; $message .= "\n"; /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */ $message .= "$body\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_message_parts--\n"; $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message\n"; $message .= "Content-Type: application/pdf;\n\tname=\"" . $name . "\"\n"; $message .= "Content-Transfer-Encoding: base64\n"; $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $name . "\"\n\n"; $message .= $content; //The base64 encoded message $message .= "\n"; $message .= "------=MIME_BOUNDRY_main_message--\n"; // send the message mail($mailto, $subject, $message, $headers); } ?> Link to comment https://forums.phpfreaks.com/topic/179968-mail-with-attachment/#findComment-949502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.