SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Playing Nice with mb_send_mail()

    Hi there,

    I've been having frustrating issues with sending attachments using PHP's mb_send_mail() function. The email I receive contains the headers among everything else, with the attachment data inline. Any help is appreciated!

    The code I'm using:
    Code PHP:
    $csv = '"First Name","Surname","DOB","Postcode","Email","Level","Start Date","End Date","Accreditation Provider"' . "\n";
    $csv .= $firstname . ',' . $surname . ',' . $dateOfBirth . ',' . $postCode . ',' . $email . ',Junior,01/01/' . date('Y', time()) . ',31/12/' . (date('Y', time()) + 4) . ',SOD';
     
    $to = 'xxx@xxx.com'
    $from = 'XXX <noreply@xxx.com>';
    $subject = 'Test Email';
     
    $attach = 'registration.csv';
    $attachtype = 'application/vnd.ms-excel';
    $attachdata = $csv;
     
    $boundary = '----=_Boundary_' . md5(time());
    $mime_boundary = '----=_MIME_Boundary_' . sha1(time());
     
    $headers  = 'From: ' . $from . "\n";
    $headers .= 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-Type: multipart/mixed;' . "\n";
    $headers .= "\t" . 'boundary="' . $mime_boundary . '"' . "\n\n";
     
    $message  = 'This is a multi-part message in MIME format.' . "\n\n";
    $message .= '--' . $mime_boundary . "\n";
    $message .= 'Content-Type: multipart/alternative;' . "\n";
    $message .= "\t" . 'boundary="' . $boundary . '"' . "\n\n\n";
    $message .= '--' . $boundary . "\n";
    $message .= 'Content-Type: text/plain;' . "\n";
    $message .= "\t" . 'charset="us-ascii"' . "\n";
    $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
    $message .= 'A new user has registered. Please find attached the user and course details.' . "\n\n\n";
    $message .= '--' . $mime_boundary . "\n";
    $message .= 'Content-Type: ' . $attachtype . ';' . "\n";
    $message .= "\t" . 'name="' . $attach . '"' . "\n";
    $message .= 'Content-Transfer-Encoding: quoted-printable' . "\n";
    $message .= 'Content-Disposition: attachment;' . "\n";
    $message .= "\t" . 'filename="' . $attach . '"' . "\n\n";
    $message .= $attachdata . "\n";
    $message .= '--' . $mime_boundary . "\n\n\n";
     
    mb_send_mail($to, $subject, $message, $headers);

    The email received:
    Code:
    This is a multi-part message in MIME format.
    
    ------=_MIME_Boundary_4a7eae3bdf6b7eddf3d1523e69e5152e3c7bb9ba
    Content-Type: multipart/alternative;
    	boundary="----=_MIME_Boundary_4a7eae3bdf6b7eddf3d1523e69e5152e3c7bb9ba"
    
    
    ------=_MIME_Boundary_4a7eae3bdf6b7eddf3d1523e69e5152e3c7bb9ba
    Content-Type: text/plain;
    	charset="us-ascii"
    Content-Transfer-Encoding: 7bit
    
    A new user has registered. Please find attached the user and course details.
    
    
    ------=_MIME_Boundary_4a7eae3bdf6b7eddf3d1523e69e5152e3c7bb9ba
    Content-Type: application/vnd.ms-excel;
    	name="registration.csv"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: attachment;
    	filename="registration.csv"
    
    "First Name","Surname","DOB","Postcode","Email","Level","Start Date","End Date","Accreditation Provider"
    dd,jjj,1987-11-11,2233,p@ddd.com,Junior,01/01/2009,31/12/2013,SOD
    
    ------=_MIME_Boundary_4a7eae3bdf6b7eddf3d1523e69e5152e3c7bb9ba
    Thank you!

    Alcar...

  2. #2
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,100
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is a quote directly from php.net.

    "Be careful, mb_send_mail chokes if you try and send a multipart e-mail"
    What I lack in acuracy I make up for in misteaks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •