Proper Email MIME

Discussion in 'PHP' started by IGiveMoney, Aug 22, 2008.

  1. #1
    This is driving me nuts and i think I just need
    a fresh set of eyes to look at this....


    Basically I have a script that is grabbing data
    from the form, and the file (if attached) and doing
    the email (without spam)

    All seems to be good.

    The part I cant get figured out is the $message.

    I've done something wrong and it's driving me nuts.

    I have a ton of post variables that need to be filled
    into the email that get sent with the attachment
    (if there is one)

    And when I receive the email I am getting the
    special characters like \n or \t

    Im trying to get it do list my vars nicely:

    Name: John Doe
    Email:
    Address: 123 main st

    And so on...

    Heres my form code on the receiving end:

    
    
    // Generate a boundary string
     $semi_rand = md5(time());
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    // Add the headers for a file attachment
      $headers .= "\nMIME-Version: 1.0\n" .
     "Content-Type: multipart/alternative;\n" .
    " boundary=\"{$mime_boundary}\"";
    
      // Add a multipart boundary above the plain message
    
     $message = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
      "Content-Type: multipart/mixed; charset=\"iso-8859-1\"\n" .
       "Content-Transfer-Encoding: 7bit\n\n " .
    $message . "\n\n"; 
    
    
       // Base64 encode the file data
       $data = chunk_split(base64_encode($data));
    
     // Add file attachment to the message
       $message .= "--{$mime_boundary}\n" .
                   "Content-Type: {$fileatt_type};\n" .
                   " name=\"{$fileatt_name}\"\n" .
                   "Content-Transfer-Encoding: base64\n\n" .
                   $data . "\n\n" .
                   "--{$mime_boundary}--\n";
     }
    
     // Send the message
     $ok = @mail($to, $subject, $message, $headers);
    
    
    
    PHP:
    My outbound messages (from the form)
    are: enctype="multipart/form-data"


    So I am not sure whats going on - can anyone tell
    me where im FUDGING UP?
     
    IGiveMoney, Aug 22, 2008 IP
  2. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Does this default to a HTML email when in multi mode, so you would need to use nl2br() on the message to convert \n etc to <br> this way the email would be formatted correctly.
     
    lfhost, Aug 22, 2008 IP
  3. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    From your site. <br>test\n\ntest


    thats what i get in the email....

    IM ABOUT READY TO KICK MY STUPID COMPUTER!
     
    IGiveMoney, Aug 22, 2008 IP
  4. pablo

    pablo Well-Known Member

    Messages:
    301
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    110
    #4
    try single quotes for the \n parts .
     
    pablo, Aug 22, 2008 IP
  5. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $message = 'From your site. ' . '<br>' . 'test'. '\n\n' . 'test';

    Thats what produced me the above example

    I was trying to format $message before it was called. Instead
    I am still getting the same results....

    Bonkers anyone?! (BOING BOING BOING!)
     
    IGiveMoney, Aug 22, 2008 IP
  6. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you cant use single quotes as the \n wont be parsed in the message as a line break.
     
    lfhost, Aug 22, 2008 IP