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
    Aug 2011
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sending mail with attachment

    Hello,
    I'm trying to use the code below to send mails with attachment. My mail gets sent but the file has a 0KB size. Any help with what's wrong will be appreciated.


    PHP Code:
    <?php
        
    if(isset($_POST['submit']))
        {

          
    //Deal with the email
          
    $to 'careers@maltontech.com';
          
    $subject 'CV Submission';
          
    $message "Kindly find attached a copy of my CV for your use.";

          
    $name strip_tags($_POST['name']);
          
    $attachment chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
          
    $filename $_FILES['file']['name'];

          
    $boundary =md5(date('r'time())); 

          
    $headers "From: \r\nReply-To: ";
          
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

          
    $message="This is a multi-part message in MIME format.

    --_1_$boundary
    Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

    --_2_$boundary
    Content-Type: text/plain; charset=\"iso-8859-1\"
    Content-Transfer-Encoding: 7bit

    $message

    --_2_$boundary--
    --_1_$boundary
    Content-Type: application/pdf; name=\"$filename\" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 

    $attachment
    --_1_$boundary--"
    ;

          
    mail($to$subject$message$headers);
        }
    ?>

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Your code is not testing for any upload errors. Are you sure there is anything to attach?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    Your code is not testing for any upload errors. Are you sure there is anything to attach?
    Thanks for the reply. Yeah, I normally want to complete a process successfully before adding error checks.
    however I manually check error by dumping at various sections of the script.

    I dumped the $_FILES array and got this:

    PHP Code:
    array(1) { ["file"]=> array(5) { ["name"]=> string(36"Quickteller -Transfer to Account.pdf" ["type"]=> string(15"application/pdf" ["tmp_name"]=> string(18"/var/tmp/phpYY5U6m" ["error"]=> int(0) ["size"]=> int(80091) } } 
    seems to me a file is posted.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    It's generally easier and more assured to work, if you use one of the existing php mailer classes, rather than trying to roll your own mail building code.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    It's generally easier and more assured to work, if you use one of the existing php mailer classes, rather than trying to roll your own mail building code.
    +1

    http://phpmailer.worxware.com/
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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