1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to attach word, excel or pdf file with php mail script

Discussion in 'PHP' started by dineshsingh1984, Oct 11, 2012.

  1. #1
    how to attach word, excel or pdf file with php mail script
     
    dineshsingh1984, Oct 11, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
  3. Red Swordfish Media

    Red Swordfish Media Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actually, the easiest way to send a file using mail() is to simply include a link in the $message variable. It might look like this.

    $subject = "Here is an email with a link to a file";
    $message = "Thank you for visiting our site. Please see the <A href="http://www.yourDomain.com/yourFileName.xls">attached file</a> to review the document that you requested.";
    $headers = 'From: ' . "\r\n" .
    'Reply-To: ' . "\r\n" .
    'Content-Type: text/html; charset="utf-8"' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $returnpath = "-fYourName@yourDomain.com";
    mail($to, $subject, $message,$headers,$returnpath);

    In this way you can avoid having to email the file. If you actually need to attach the file then you might want to use the sample code for sending an email with attachment that is posted at webcheatsheet dot com. It shows how to set the content-type and pass in the file.
     
    Red Swordfish Media, Oct 11, 2012 IP
  4. phpexperts

    phpexperts Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #4
    Below is the code for file attachment in mail.


    <?php
    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 .= "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; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
    echo "mail send ... OK"; // or use booleans here
    } else {
    echo "mail send ... ERROR!";
    }
    }

    $my_file = "somefile.zip";
    $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
    $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, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
    ?>
     
    phpexperts, Oct 12, 2012 IP
  5. kevinn13

    kevinn13 Greenhorn

    Messages:
    24
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    11
    #5
    I would agree with Red Swordfish use the link as it will speed up delivery and most modern day spam filters will block emails sent using PHP mailer as they are mostly deemed spammy let alone with attachments. Check out SMTP email class if you want to send these items securely using attachment.
     
    kevinn13, Oct 12, 2012 IP