Jump to content

[SOLVED] Mail with attachment problem


robert_gsfame

Recommended Posts

Below is the code and it didn't work well as file uploaded not in word document, Moreover, i always find the file attached twice (DOUBLE). Another question is that how can i have 2 attachment let say one in word and the other in JPEG format.

 

Please add more code as i'm not familiar with this. Thx in advance

 

$file="upload_file/".$document;

 

 

    $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<" . $email . ">\n";

    $headers .= "Reply-To: <" . $email . ">\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<" . $email . ">\n";

    $headers .= "X-Priority: 1\n"; //1 = Urgent, 3 = Normal

    $headers .= "Return-Path: <" . $email . ">\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";

    $message .= "$message\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 .= $message; //The base64 encoded message

    $message .= "\n";

    $message .= "------=MIME_BOUNDRY_main_message--\n";

 

$mailto = $recipient_email;

$subject = "mymessage";

 

    // send the message

    mail($mailto, $subject, $message, $headers);

 

 

 

 

Link to comment
Share on other sites

okay i've modified the code into below:

 

$to = $email;

$from = $from_name;

$subject ="Hello world";

$message = "Nice Weather";

$headers = "From: firstname_lastname@yahoo.com";

 

// boundary

$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

 

// headers for attachment

$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

 

// multipart 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";

$message .= "--{$mime_boundary}\n";

 

$file = fopen($files,"rb");

$data = fread($file,filesize($files));

fclose($file);

$data = chunk_split(base64_encode($data));

$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files\"\n" .

"Content-Disposition: attachment;\n" . " filename=\"$filename\"\n" .

"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";

$message .= "--{$mime_boundary}\n";

 

mail($to, $subject, $message, $headers);

}

 

THis code works, but this part (From:) i got this firstname@yahoo.com, it should be firstname_lastname@yahoo.com

 

Secondly in which part, should i modify to attach more than one file??

 

thanx

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.