0

I'm trying to use PHP to send an email with an attached file. I've followed the excellent tutorial at http://webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment and made the following changes:

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/txt; name="<?php echo $_POST[log_file]?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

Neither the original script or my amended script seems to attach any files to my emails. Anyone have any idea what I'm doing wrong.

Note: the files I'm attaching to my form are all text files (various file extensions e.g. .log, .txt etc).


OK, I've now proven I think that the MIME encoding part of this script isnt working. I've placed a text file in my upload directory and changed the script to read:

--PHP-mixed-<?php echo $random_hash; ?>   
Content-Type: text/plain; name="<?php echo $_SERVER['DOCUMENT_ROOT']. '/upload/test.txt' ?>" 
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

When looking at the email source on receipt I see the full path and document name under Content-Type: text/plain; name=, but theres no attachment, encoded or otherwise.

Anyone know how I get this to work?

I've tried the following:

--PHP-mixed-<?php echo $random_hash; ?>   
Content-Type: text/plain; name="<?php echo $_FILES['file']['name']; ?>"
Content-Transfer-Encoding: base64  
Content-Disposition: attachment

This presents the file name in the email source, but no attachment, interestingly:

--PHP-mixed-<?php echo $random_hash; ?>   
Content-Type: text/plain; name="<?php echo $_FILES['file']['temp_name']; ?>"
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

Returns an empty file name and still not attachement, I can only assume the temp file has bee deleted by the time I execute this?

Taffman
  • 79
  • 1
  • 6
  • see if this helps: http://stackoverflow.com/questions/6275070/php-mail-attachment-problems – Maximus2012 Jul 23 '13 at 15:40
  • Is the use of: name="" valid or do I have to store the file somewhere in a temporary location and refer to that location for retrieval before sending? – Taffman Jul 23 '13 at 17:12
  • it looks valid. you just need to make sure that there is valid value in there ($_POST['log_file']) – Maximus2012 Jul 23 '13 at 17:14
  • @ Maximus2012 Thanks for this, but it made no difference. Looking at the received email source I see the following: --PHP-mixed-edc3b09aea5aa11614b07dadbf00098f Content-Type: application/txt; name="TEST.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment It seems to be finding the file name from my form i.e. $_POST[log_file] but it's not attaching/encoding the file? – Taffman Jul 24 '13 at 09:52
  • I think my form file input field

    Attach Log File: 

    isn't storing the file anywhere so the mail script cant find it so encode. Anyone know how I define a default location in PHP for the form to store the files?
    – Taffman Jul 24 '13 at 10:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34071/discussion-between-taffman-and-maximus2012) – Taffman Jul 24 '13 at 13:07
  • did you try the working solutions from the other stackoverflow question? is your mail server configured properly ? – Maximus2012 Jul 24 '13 at 13:34
  • I've been looking at http://stackoverflow.com/questions/3945330/php-filestmp-name and others, but no real clue as to why I dont recieve an attachemnt yet? – Taffman Jul 24 '13 at 16:10
  • are you getting any specific error message? anything in the server log maybe ? its also possible that there might be error in some other parts of your application. – Maximus2012 Jul 24 '13 at 16:11

1 Answers1

0

OK finally got this working by moving the temp file to another location on my server, then encoding that file.

Not sure why I can't encode the temp file without saving it, but at least I got it working now.

Thanks to everyone who took the time to reply to this, you help me a lot.

Taffman
  • 79
  • 1
  • 6