SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    38
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    email attachments

    Hello, I can't get email attachments to work correctly. I have tried every combination that I can think of. Instead of sending it as an attachment it sends it as gobbledeygook within the text of the email. Although the email shows that it has an attachment before it is opened.

    -------------------------------------------------------------------------------

    I have a function:

    function send_comms_att (
    $p_comm_type,
    $p_from,
    $p_to,
    $p_subject,
    $p_text,
    $p_attach_file )
    {

    // read the attachment file contents into a string,
    // encode it with MIME base64,
    // and split it into smaller chunks

    $attachment = chunk_split( base64_encode( file_get_contents( $p_attach_file ) ) );

    // create a boundary string. It must be unique
    // so we use the MD5 algorithm to generate a random hash

    $random_hash = md5( date( 'r', time() ) );


    // define the headers we want passed.


    $headers1 = "From:" . $p_from . "\r\n";

    $headers2 = "MIME-Version: 1.0\r\n";

    $headers3 = "Content-Type: multipart/mixed; boundary=\"PHP-mixed-" . $random_hash . "\r\n";

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

    $headers5 = "filename=\"" . $attachment . "\"";

    $headers = $headers1 . $headers2 . $headers3 . $headers4 . $headers5;



    // send the email

    $mail_sent = @mail( $p_to, $p_subject, $p_text, $headers );

    if ( !$mail_sent ) {

    $comm_status = 1;

    } else {

    $comm_status = 0;

    }


    return $comm_status;

    }
    -------------------------------------------------------------------------------

    I have some code to test the function:

    <?php
    $pdf_file = 'pdf_file.pdf';

    require 'fpdf.php';

    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello. I am a PDF!');
    $pdf->Output($pdf_file);

    $mail = send_comms_att (
    'E',
    'admin@Dark-Mantra.co.uk',
    'admin@Dark-Mantra.co.uk',
    'Test',
    'Hello',
    $pdf_file );

    echo '<p>Status ' . $mail . '</p>';


    ?>

    -------------------------------------------------------------------------------


    Here is the gibberish email thus sent:

    IDAgUgovQ29udGV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GFnZUxheW91dCAv
    T25lQ29sdW1uCj4+CmVuZG9iagp4cmVmCjAgOAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAy
    MzQgMDAwMDAgbiAKMDAwMDAwMDQyMiAwMDAwMCBuIAowMDAwMDAwMDA5IDAwMDAwIG4gCjAwMDAw
    MDAwODcgMDAwMDAgbiAKMDAwMDAwMDMyMSAwMDAwMCBuIAowMDAwMDAwNTI2IDAwMDAwIG4gCjAw
    MDAwMDA2MDEgMDAwMDAgbiAKdHJhaWxlcgo8PAovU2l6ZSA4Ci9Sb290IDcgMCBSCi9JbmZvIDYg
    MCBSCj4+CnN0YXJ0eHJlZgo3MDQKJSVFT0YK
    "

    Hello

    -------------------------------------------------------------------------------


    Thanks,

    Steve

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use some sort of Mail class, like the Mail_Mime from pear. Coding this by hand is prone to errors because email servers are very sensitive the incorrectly set line breaks.

    Packages like Mail_Mime provides interface to adding attachments, so the class will do all the work for you.

    See how easy it would be to write the code if you were using Mail_Mime:
    http://pear.php.net/manual/en/packag...me.example.php
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    38
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for this.

    I have looked at Mail_Mime and also a few others.

    I like the look of Phpmailer ( In truth it's the only one I understand ), so I have downloaded it. One thing though. I don't like being too reliant on third party software that might only have a limited lifespan. So I would encompass it in my own function. What are your thoughts on this?

    Steve


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
  •