SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Threaded View

  1. #1
    Non-Member
    Join Date
    Jun 2013
    Location
    Patna
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to Send mail with an attachment in PHP

    i would like to send a mail with an attachment with the php mail function. I can receive a mail. Unfortunately the image is broken. The image is in the same directory as the php script.

    My source code for the script:

    PHP Code:
    $name "Name goes here";
    $email "recipient@gmail.com";
    $to "$name <$email>";
    $from "Sender";
    $subject "Here is your attachment";
    $fileatt "test.jpg";
    $fileatttype "image/jpeg";
    $fileattname "test.jpg";
    $headers "From: $from";

    // File
    $file fopen($fileatt'rb');
    $data fread($filefilesize($fileatt));
    fclose($file);

    // This attaches the file
    $semi_rand md5(time());
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    $message "This is a multi-part message in MIME format.\n\n" .
    "-{$mime_boundary}\n" .
    "Content-Type: text/plain; charset=\"iso-8859-1\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $message .= "\n\n";

    $data chunk_split(base64_encode($data));
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatttype};\n" .
    " name=\"{$fileattname}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileattname}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data "\n\n" .
    "-{$mime_boundary}-\n";

    // Send the email
    if(mail($to$subject$message$headers)) {

        echo 
    "The email was sent.";

    }
    else {

        echo 
    "There was an error sending the mail.";


    Last edited by cpradio; Jun 24, 2013 at 04:32. Reason: Added php tags


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •