Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

(PHP) Email attachment

Options
  • 24-03-2007 12:50am
    #1
    Registered Users Posts: 4,475 ✭✭✭


    I'm trying to send an email via PHP's mail() command with a file attachment. Strangely, the file attaches just fine, but there's no message, just the attachment. Can anyone spot anything I've done wrong?
    $headers  = "From: $from\r\n"; 
    $headers .= "Reply-To: $from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "X-Sender: $from\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; 
    $headers .= "Return-Path: <$from>\n"; 
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\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/html; charset=\"iso-8859-1\"\n"; 
    $message .= "Content-Transfer-Encoding: quoted-printable\n"; 
    $message .= "\n"; 
    
    $message .= "message goes here\n";
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_message_parts--\n"; 
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message\n"; 
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n"; 
    $message .= "------=MIME_BOUNDRY_main_message--\n";
    


Comments

  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Have you tested this with different mail programs?


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Thanks, but after checking other php code, it seems that I got the headers a bit mucked up. Got it fixed now. Cheers


  • Closed Accounts Posts: 22,479 ✭✭✭✭philologos


    can you show us the correct version by any chance. What are you intending to do with this? make an email client or something. The imap functions can get down POP3 mail so it would be a good idea. :)


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Jakkass wrote:
    can you show us the correct version by any chance. What are you intending to do with this? make an email client or something. The imap functions can get down POP3 mail so it would be a good idea. :)
    I have the following working:
    $headers  = "From: $l_from\r\n"; 
    $headers .= "Reply-To: $l_from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "X-Sender: $l_from\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <$l_from>\n"; 
    
    $boundary = "====_xxxx_mail_generator_" . md5(uniqid(srand(time()))) . "====";  
    $sMIME = getMIMEType($attach);
    		
    $headers .= "Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n";  
        
    $message = "\nThis is a multi-part message in MIME format.\n" ;
    $message .= "\n--$boundary\n";
    $message .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\n\n";
    
    $message .= "$body\n";
    $message .= "\n--$boundary\n";
    $message .= "Content-Type: $sMIME;\n";
    $message .= "\tname=\"$attach_name\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n";
    $message .= "\tfilename=\"$attach_name\"\n\n";
    $message .= $data . "\n";
    $message .= "\n\n--$boundary--";
    
    The code is only in use for a client so that users can send attachments to them via email.


Advertisement