Embedding images into email body

Discussion in 'PHP' started by briansanz, Nov 23, 2009.

  1. #1
    What's wrong with my php code? I'm trying to embed an imagen into an email using php..

    $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

    // open and read the file as binary
    $file = fopen("activesearch.jpg",'rb');
    $data = fread($file,filesize("activesearch.jpg"));
    fclose($file);

    // encode and split it into acceptable length lines
    $data = chunk_split(base64_encode($data));

    // message headers
    $headers = "From: Brian <brian@domain.com>\r\n";
    $headers .= "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/mixed;\r\n" .
    " boundary=\"{$mime_boundary}\"";


    // message body
    $message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-type: text/html; charset=us-ascii\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_body . "\n\n";

    // insert a boundary to indicate start of the attachment
    // specify content type, file name, and attachment
    // then add file content and set another boundary
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: image/jpeg;\n" .
    " name=\"archivesearch.jpg\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    "Content-ID: <image_identifier>\r\n".
    "Content-Disposition: inline; filename:\"activesearch.jpg\"\r\n".
    $data . "\n\n" .
    "--{$mime_boundary}--\n";


    mail("myemail@domain.com", $subject, $message, $headers);
     
    briansanz, Nov 23, 2009 IP
  2. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use My function to sent HTML emails :D
    and don't forget to REP++ me up..

    function sendHTMLemail($HTML,$from,$to,$subject,$bcc)
    {
    // First we have to build our email headers
    // Set out "from" address
    
        $headers = "From: $from\r\n";
    
    //bcc
        $headers .= "Bcc: $bcc\r\n";
    
    // Now we specify our MIME version
    
        $headers .= "MIME-Version: 1.0\r\n";
    
    // Create a boundary so we know where to look for
    // the start of the data
    
        $boundary = uniqid("HTMLEMAIL");
       
    // First we be nice and send a non-html version of our email
       
        $headers .= "Content-Type: multipart/alternative;".
                    "boundary = $boundary\r\n\r\n";
    
        $headers .= "This is a MIME encoded message.\r\n\r\n";
    
        $headers .= "--$boundary\r\n".
                    "Content-Type: text/plain; charset=utf-8\r\n".
                    "Content-Transfer-Encoding: base64\r\n\r\n";
                   
        $headers .= chunk_split(base64_encode(strip_tags($HTML)));
    
    // Now we attach the HTML version
    
        $headers .= "--$boundary\r\n".
                    "Content-Type: text/html; charset=utf-8\r\n".
                    "Content-Transfer-Encoding: base64\r\n\r\n";
                   
        $headers .= chunk_split(base64_encode($HTML));
    
    // And then send the email ....
    
        mail($to,$subject,"",$headers);
       
    }
    PHP:
     
    xenon2010, Nov 24, 2009 IP