Page 1 of 1

Image Attachment

Posted: Fri Oct 01, 2010 2:27 pm
by todd2007
Hello,

I am trying to sent an image as an attachment in the email.

Below is my code but it doesnt attach the image. It doesnt give any error codes I get the email and there is no attachment.

Code: Select all


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)) {

    } else {

    }
}



{
if($PROD_ID == "160")
{
$my_file = "one.png";
$my_path = "/var/www/vhosts/one.com/httpdocs/images/";

}

if($PROD_ID == "161")
{
$my_file = "two.png";
$my_path = "/var/www/vhosts/one.com/httpdocs/images/";
}


$to1=$replyemail;
$my_name = "Sam";
$my_mail = "sam@sam.com";
$my_replyto = $replyemail;
$subject1 = $prod_email_subject;
$message1 = $prod_email_message;
mail_attachment($my_file,$my_path,$to1, $my_mail, $my_name, $my_replyto, $subject1, $message1);

}

can someone tell me what i am doing wrong

todd

Re: Image Attachment

Posted: Fri Oct 01, 2010 9:21 pm
by requinix
$headers is only supposed to have the headers for the email. What you have now is actually the headers and the email.

Code: Select all

    $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";
All that is the headers. Remove the trailing \r\n\r\n and you're done.

Code: Select all

    $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."--";
All that is the body of the email.