Jump to content

Mail with attachment


robert_gsfame

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.