Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Sending Attachments through PHP Script

    I am trying to send an email using PHP script but it seems that the attachments wants to convert to text and embed into the email.

    this is the code, not sure why its embedding.

    $fileatt = $_FILES['attach']['tmp_name'];
    $fileatt_name = basename($_FILES['attach']['name']);
    $fileatt_type = substr($fileatt_name, strrpos($fileatt_name, '.') + 1);


    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .

    $body .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .

    $body .= "\n\n";

    $data = chunk_split(base64_encode($data));

    $body .= "-{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $body .= "\n\n" .
    "-{$mime_boundary}-\n";

    mail($email_to, $subject, $body, $headers);

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Use the phpmailer class instead (find it via google). It takes all the hassle out of doing stuff like this the hard way.
    I can't really think of anything to write here now...

  • #3
    New Coder
    Join Date
    Mar 2012
    Location
    Ontario, Canada
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts
    OK, I got the PHPMailer working. Since I pull all of the names from a Database I had to use the AddBCC line.

    Is there a way to clear the AddAddress, every time I go through the script? So their names show up in the To line and ONLY their email address shows up there? This way there is no distribution of an entire list

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Yes - ClearAddresses()

    If you use notepad++ it has an option in the 'view' menu called 'Fold all'.

    Click that and then click the little + symbol by the line numbers. It will expand the class file so you can see the function names only without the function code. It makes it a lot easier to see what functions are available to you.
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    chellert (11-17-2012)

  • #5
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    This is a pretty good and popular tutorial on how you can send plain text emails, html emails and emails with attachments from webcheatsheets.com.

    http://webcheatsheet.com/php/send_em...attachment.php


  •  

    Posting Permissions

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