Jump to content



Photo

PHP File Form Attachment script?


22 replies to this topic

#1 HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 01 August 2014 - 03:30

I already have the php down for form submitting, however I don't know what is the code for php for file attachments straight to email, without uploading to server. I also need the script to specify that it has to be a docx or a pdf. Anybody can help?




#2 Cocoliso

Cocoliso

    Neowinian

  • Joined: 13-July 09

Posted 01 August 2014 - 03:52

PHP stores the temp path inside this array $_FILES['uploaded_file']['tmp_name'].

 

Make sure to check that file uploaded correctly in order to not attach corrupted files eg:

 

if ($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK) { }

 

It's a temporary file so it should be deleted at the end of the script, so make sure to move or copy to somewhere else in case you want to keep it on your server.

 

Note: Although sending emails through PHP is pretty straight forward I normally use PHPMailer to send emails it makes my life a little more easier :) (https://github.com/PHPMailer/PHPMailer)

 

Hope this helps.



#3 Cocoliso

Cocoliso

    Neowinian

  • Joined: 13-July 09

Posted 01 August 2014 - 03:57

Oh also don't forget to include this attribute in your form enctype="multipart/form-data"



#4 OP HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 01 August 2014 - 11:26

Is there is a script so it doesn't upload to the server, but instead just sends the email as attachment?



#5 +virtorio

virtorio

    4089 III

  • Tech Issues Solved: 11
  • Joined: 28-April 03
  • Location: New Zealand
  • OS: OSX 10.9, Windows 8.1
  • Phone: Samsung Galaxy SIII

Posted 01 August 2014 - 11:46

No. 



#6 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 22
  • Joined: 23-January 11
  • Location: Netherlands
  • OS: Windows 8.1 Pro
  • Phone: Nexus 5

Posted 01 August 2014 - 15:04

Is there is a script so it doesn't upload to the server, but instead just sends the email as attachment?

Technically impossible to attach a file without uploading it, you must upload it to the server first so you can work with it in php.

 

Though you can encode the file in base64 with javascript on form submission and then use the decoded base64 string as a mail attachment but that will be complex and slower.



#7 shozilla

shozilla

    Neowinian Senior

  • Tech Issues Solved: 6
  • Joined: 11-January 09

Posted 01 August 2014 - 15:11

Is there is a script so it doesn't upload to the server, but instead just sends the email as attachment?

 

No it won't work that way.... Pictures, Text, Documents, and zip are sent to the server and wait for the addressee to open the email you sent to so he/she can download said file from the server.

 

Same way that you create website and upload to the server ... so your visitors can go to your website to view your content you made.. that they are sitting in your server...

 

Even you have a phone that you want to send a text to someone... the text message can be sent to the server to sit there until someone on other end to open it up and read it.



#8 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 22
  • Joined: 23-January 11
  • Location: Netherlands
  • OS: Windows 8.1 Pro
  • Phone: Nexus 5

Posted 01 August 2014 - 18:04

Here you go http://webcheatsheet....php#attachment

Replace base64_encode(file_get_contents('attachment.zip')) with base64 string created with js.

 

To get base64 string with js from html form: http://jsfiddle.net/eliseosoto/JHQnk/

 

Make sure to check filesize against max filesize with js first before calculating the base64 string, else the browser will hang because the file might be too big.



#9 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 22
  • Joined: 23-January 11
  • Location: Netherlands
  • OS: Windows 8.1 Pro
  • Phone: Nexus 5

Posted 01 August 2014 - 18:12

No it won't work that way.... Pictures, Text, Documents, and zip are sent to the server and wait for the addressee to open the email you sent to so he/she can download said file from the server.

 

Same way that you create website and upload to the server ... so your visitors can go to your website to view your content you made.. that they are sitting in your server...

 

Even you have a phone that you want to send a text to someone... the text message can be sent to the server to sit there until someone on other end to open it up and read it.

The file will be on the mail server of the receiver not the sender when you send a email attachment. Feel free to test it, send a file with a attachment from a server and then take the server down, the receiver can still download the attachment.



#10 OP HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 03 August 2014 - 22:55

So I tried as posted above, wont work. this is the current php code I have:

<?php

$emailTo     = myemail@.com;
$emailFrom   = "noreply@yourdomain.com ";
$subject     = "subject I had";
$redirectURL = "confirm.html

foreach ($_POST as $key => $value) {
  $keyInfo = explode("__", $key);
 
  if ($keyInfo[0] == "INFO") {
    $message .= $keyInfo[1].":\n ".$value."\n\n";
  }

}

mail($emailTo, $subject, $message, "From: ".$emailFrom);

header("Location: $redirectURL");

//Get the uploaded file information
$name_of_uploaded_file =
    basename($_FILES['uploaded_file']['name']);
 
//get the file extension of the file
$type_of_uploaded_file =
    substr($name_of_uploaded_file,
    strrpos($name_of_uploaded_file, '.') + 1);
 
$size_of_uploaded_file =
    $_FILES["uploaded_file"]["size"]/1024;//size in KBs

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"];
 }

?>

 

this is the html:

<form action="process_form2.php" method="post" enctype="multipart/form-data">

<input type="file" name="file" id="file">



#11 shozilla

shozilla

    Neowinian Senior

  • Tech Issues Solved: 6
  • Joined: 11-January 09

Posted 03 August 2014 - 23:06

The file will be on the mail server of the receiver not the sender when you send a email attachment. Feel free to test it, send a file with a attachment from a server and then take the server down, the receiver can still download the attachment.

 

I know that.. that's what I said... the addressee also means receiver.



#12 +theblazingangel

theblazingangel

    Software Engineer

  • Tech Issues Solved: 4
  • Joined: 25-March 04
  • Location: England, UK

Posted 04 August 2014 - 00:04

So I tried as posted above, wont work. this is the current php code I have:

<snip>

 

What won't work? Perhaps if you elaborated on this then someone may be able to help you fix what it is that's not working for you.

 

A few pointers though for the biggest mistakes you're making:

1) In the middle of your code you're redirecting the user's browser to another page, so they're never going to see the output created following this.

2) You're not bothering to check if the mail() function actually succeeded.

3) You're not actually attaching the file to the email. Where you expecting it to be or are you actually aware that this is the case currently, and you're just trying to get a simple email without an attachment working first before plumbing the attachment stuff in?



#13 OP HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 04 August 2014 - 00:42

What won't work? Perhaps if you elaborated on this then someone may be able to help you fix what it is that's not working for you.

 

A few pointers though for the biggest mistakes you're making:

1) In the middle of your code you're redirecting the user's browser to another page, so they're never going to see the output created following this.

2) You're not bothering to check if the mail() function actually succeeded.

3) You're not actually attaching the file to the email. Where you expecting it to be or are you actually aware that this is the case currently, and you're just trying to get a simple email without an attachment working first before plumbing the attachment stuff in?

 

 

 

1) Yes I know

2) How do I do that?

3) That's whats not working, what am I missing.



#14 adrynalyne

adrynalyne

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 29-November 09

Posted 04 August 2014 - 00:55

I went through this particularly painful issue a while back. It convinced me to use swiftmailer.



#15 +theblazingangel

theblazingangel

    Software Engineer

  • Tech Issues Solved: 4
  • Joined: 25-March 04
  • Location: England, UK

Posted 04 August 2014 - 01:18

1) Yes I know

2) How do I do that?

3) That's whats not working, what am I missing.

 

Ok, for #2:

if (!mail($emailTo, $subject, $message, "From: ".$emailFrom)) {
    echo "<p>Error: mail function failed to send email!</p>";
}

For #3, you need to add the correct headers (not just the from field), format the message part of the email correctly and actually include an encoded copy of the file in it.

You can see an example of how to do this correctly here: http://www.excellent...bmit-using-php/

 

In the example linked above, note:

  • They're adding additional headers - mime version, content type and boundary
  • They're adding all of the correct and necessary bits of meta data into the message body. (Understand that this is not stuff that the recipient will see when they view the email, not unless the recipient views it in raw source mode, or whatever their mail client calls it. Think of viewing an email in source mode like seeing the html source behind an html webpage).
  • They're encoding a copy of the file correctly with the base64_encode() and chunk_split() functions and then adding that output as part of the message.

 

To add additional text content to the message, from a brief look at the code linked above, I think the correct place would be between the 'Multipart Boundary above message' and 'Encoding file data' sections.