11 Replies - 10730 Views - Last Post: 23 January 2012 - 11:54 AM Rate Topic: -----

#1 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Sending attachment via php mail() with temp file on server

Posted 05 January 2012 - 05:13 PM

Hi all,

I've managed to be able to upload a temp file to the server after using the file upload function
but unsure on the next step of turning this into an attachment and sending it in an e-mail.

My idea is as followed:

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "You did not add your file.";
  die();
  } 
if (($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
&& ($_FILES["fileToUpload"]["size"] < 20480))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
	
	
$fname = "Joe";
$sname = "Bloggs";
$email = $_POST["email"];
$mail ="XXXXX@hotmail.co.uk";

//Sorting out the email to be sent to the user
     $email_message  = "Test: ". $_FILES["file"];
	 
	   $mail = mail($mail,"Request From ".$fname." ".$sname,$email_message,"From: ".$email." <".$name." ".$sname.">");
    }
  }
else
  {
  echo "You tried to send a file that wasn't a word document. Please try again.";
  }
 
?> 



As you can guess it didn't work. Unsure on the next step though. Any help would be great

Is This A Good Question/Topic? 0
  • +

Replies To: Sending attachment via php mail() with temp file on server

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Sending attachment via php mail() with temp file on server

Posted 05 January 2012 - 10:18 PM

Here's a tutorial demonstrating how one would add JPG files as an email. The same principle applies to all other files. The only change is you must update the content type to suit the file you're attaching.
HTML EMAIL WITH JPEG ATTACHMENTS TUTORIAL
Was This Post Helpful? 1
  • +
  • -

#3 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Sending attachment via php mail() with temp file on server

Posted 07 January 2012 - 02:54 PM

Hi Codeprada,

Followed the link you sent and edited to try and suit my needs but each time I just get mail failed.

I've got it so when someone adds their file and clicks the link, it will send it to the server temporarly and this works, the e-mail not so good:


if ($_FILES["file"]["error"] > 0)
  {
  echo "You did not add your CV file.";
  die();
  } 
if (($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
&& ($_FILES["fileToUpload"]["size"] < 20480))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {

//email section

	$filename = $_FILES["file"]["tmp_name"];
	 
$to = 	'emailhere';
$subject = 	'PHP Mail Attachment Test';
$bound_text = 	"test123";
$bound = 	"--".$bound_text."\r\n";
$bound_last = 	"--".$bound_text."--\r\n";
  	 
$headers = 	"From: admin@server.com\r\n";
$headers .= 	"MIME-Version: 1.0\r\n"
  	."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
  	 
$message .= 	"If you can see this MIME then your client doesn't accept MIME types!\r\n"
  	.$bound;
  	 
$message .= 	"Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
  	."Content-Transfer-Encoding: 7bit\r\n\r\n"
  	."hey my <b>good</b> friend here is a picture of regal beagle\r\n"
  	.$bound;
  	 
$file = 	file_get_contents($filename);  	 
$message .= 	"Content-Type: ".$_FILES["file"]["type"]."; name=\"".$filename."\"\r\n"
  	."Content-Transfer-Encoding: base64\r\n"
  	."Content-disposition: attachment; file=\"".$filename."\"\r\n"
  	."\r\n"
  	.chunk_split(base64_encode($file))
  	.$bound_last;

if(mail($to, $subject, $message, $headers))
{
     echo 'MAIL SENT';
} else {
     echo 'MAIL FAILED';
}
   
    }
  }
else
  {
  echo "You tried to send a file that wasn't a word document. Please try again.";
  }



Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Sending attachment via php mail() with temp file on server

Posted 07 January 2012 - 04:14 PM

:ohno:

Quote

e-mail not so good

Is that an error message?

With almost 400 posts you should know by now what information you need to provide if you want help.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Sending attachment via php mail() with temp file on server

Posted 07 January 2012 - 04:22 PM

BTW, my guess is that it's a problem with Content-Type. You might check what you're getting for $_FILES["file"]["type"].
Was This Post Helpful? 0
  • +
  • -

#6 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Sending attachment via php mail() with temp file on server

Posted 09 January 2012 - 01:47 PM

I echo'd what you said and it came out as: application/vnd.openxmlformats-officedocument.wordprocessingml.document

which would be right since it was a docx file...

It keeps just going to Mail Failed meaning something is wrong with the mail section of the code but not sure what exactly
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Sending attachment via php mail() with temp file on server

Posted 09 January 2012 - 05:30 PM

Remove the attachments. Can you mail a message without them?
Was This Post Helpful? 0
  • +
  • -

#8 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Sending attachment via php mail() with temp file on server

Posted 10 January 2012 - 01:04 PM

Hey CT

Thanks for your help so far!

Took your advice and after taking it apart the header section was causing problems. As soon as I deleted this from the mail function (and the header content section) it worked.

However, I now get an email with the message content and then the "file" comes in random characters, I believe 72chars per each lines, which I believe is down to:

.chunk_split(base64_encode($file));

If that's the case how do I make it so instead of the random set of chars, the actual file is sent as an attachment?

Thanks
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Sending attachment via php mail() with temp file on server

Posted 10 January 2012 - 01:14 PM

I can't test it now, but I'm guessing that there's a problem encoding the file, probably because as a text file it's already encoded. You could try it this way:
chunk_split($file)

Was This Post Helpful? 0
  • +
  • -

#10 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Sending attachment via php mail() with temp file on server

Posted 12 January 2012 - 12:34 PM

Hey CT,

I've had a look into this, the code you've put just displays another set of random chars.
Was This Post Helpful? 0
  • +
  • -

#11 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Sending attachment via php mail() with temp file on server

Posted 17 January 2012 - 11:05 AM

Anyone got any ideas?
Was This Post Helpful? 0
  • +
  • -

#12 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Sending attachment via php mail() with temp file on server

Posted 23 January 2012 - 11:54 AM

Ok.

At The Moment my code looks like this:

	$filename = $_FILES["file"]["tmp_name"];
	 
$to = 	'Matthew.Gray@hotmail.co.uk';
$subject = 	'PHP Mail Attachment Test';
  
$message .= 	"Content-Type: ".$_FILES["file"]["type"]."; name=\"".$filename."\"\r\n"
  	."Content-Transfer-Encoding: base64\r\n"
  	."Content-disposition: attachment;"
  	."\r\n"
  	.chunk_split(base64_encode(file_get_contents($filename)))
  	.$bound_last;
 
if(mail($to, $subject, $message, $from))
{
     echo 'MAIL SENT SUCCESSFULLY';
} else {
     echo 'MAIL SENDING FAILED';
}



Now finding tutorials on this shows this should work. BUT! instead of being sent as the actual file it's being sent in it's chunk_splitbase64_encode form inside the email itself.

Unsure How to convert this back to the actual document itself since all the tutorials say this method should work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1