Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

(PHP) Email attachment

  • 24-03-2007 12:50AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 6,342 ✭✭✭OfflerCrocGod


    Have you tested this with different mail programs?


  • Registered Users, Registered Users 2 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, Registered Users 2 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