Jump to content
  • 0

Question

Posted

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?

Share this post


Link to post
Share on other sites

22 answers to this question

  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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

Share this post


Link to post
Share on other sites
  • 0

Posted

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

Share this post


Link to post
Share on other sites
  • 0

Posted

No. 

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

Here you go http://webcheatsheet.com/php/send_email_text_html_attachment.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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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">

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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?

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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

Share this post


Link to post
Share on other sites
  • 0

Posted

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.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-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.

 

I added what you asked, didn't work, I then went to the site, copied the whole php from scratch and redid the form. didn't work. (as in doesn't send emails or anything)

Share this post


Link to post
Share on other sites
  • 0

Posted

I added what you asked, didn't work, I then went to the site, copied the whole php from scratch and redid the form. didn't work. (as in doesn't send emails or anything)

 

Okay, so was it sending emails before or not? When I asked earlier what exactly wasn't working, you said attachments weren't working, which seemed to suggest that emails were getting through; now you seem to be suggesting that no emails have been getting through at all... which is it?

 

If you're not getting any emails through at all with the code you've tried so far, try the stripped down example code below. Replace the $to address below with your own email address, save it as a separate script, and run it. If this doesn't work, (and you've checked your spam folder haven't you??), then your problem isn't anything to do with the php code. Either you've not got the necessary mail software installed on your server; it's not configured correctly or you don't have access; or perhaps the message is getting sent but then getting blocked somewhere.

<?php

$to = 'me@example.com'; // <-- REPLACE THIS WITH YOUR EMAIL ADDRESS!!!
$subject = 'test';
$message = 'test message';
$headers = 'From: webmaster@example.com';

if (mail($to, $subject, $message, $headers)) {
    echo "sending mail suceeded!";
} else {
    echo "sending mail failed!";
}

?>

Share this post


Link to post
Share on other sites
  • 0

Posted

If you can not get it to work, then go to the control panel and install the mail form for your website..

 

then modify the config file that fits your needs such as email address, subject, etc.

 

If you want the attachment, then add this part to your script.

 

If you are not able to get it to work, hire someone to get it to work for you.

Share this post


Link to post
Share on other sites
  • 0

Posted

 

Okay, so was it sending emails before or not? When I asked earlier what exactly wasn't working, you said attachments weren't working, which seemed to suggest that emails were getting through; now you seem to be suggesting that no emails have been getting through at all... which is it?

 

If you're not getting any emails through at all with the code you've tried so far, try the stripped down example code below. Replace the $to address below with your own email address, save it as a separate script, and run it. If this doesn't work, (and you've checked your spam folder haven't you??), then your problem isn't anything to do with the php code. Either you've not got the necessary mail software installed on your server; it's not configured correctly or you don't have access; or perhaps the message is getting sent but then getting blocked somewhere.

 

 

 

In my original post, with the php that I posted, the email was sent but file attachment was not, so I did what you suggested, I added the

 

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

 

then I added the headers. once I did that, it did not send mail nor the attachment.

Share this post


Link to post
Share on other sites
  • 0

Posted

In my original post, with the php that I posted, the email was sent but file attachment was not, so I did what you suggested, I added the

 

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

 

then I added the headers. once I did that, it did not send mail nor the attachment.

 

When you say the email was sent, just to be absolutely clear, did you receive it? Forget the attachment stuff for a moment; first we need to be absolutely clear about whether you're actually able to receive emails sent from php code running on your server. If not, then the problem isn't with the php code, and we need to start looking elsewhere. If you can receive emails but not those with attachments, then either there's a mistake in the php code you're running, or the email is getting blocked by something somewhere for some reason.

 

The php mail function, as I mentioned before, hands the email over to another piece of software which will actually perform the delivery. This mail delivery software may or may not accept the email handed over to it. When the php mail function attempts to hand the email over to it, the mail delivery software will respond to tell the php mail function whether it was accepted or not. Once this exchange is completed, the mail function returns true if the message was accepted by the mail delivery software, and false if not. Since there is never a guarantee that the mail delivery software will accept the message, it is thus a good idea to always check the return value of the mail function and print an success/failure message for the user as necessary.

 

Understand that even if the mail delivery software accepts the email and thus the php mail function returns true, resulting in your code probably informing the user that the message was sent, the php mail function knows nothing about what will happen to the message between the mail delivery software and the recipient's inbox. It may very well fail to reach it's destination.

 

To move forward with helping you solve the problems you're facing here, it would be really helpful to have very clear answers to the following questions:

  1. Have you actually received an email in your inbox sent from php code you've run? Forget attachments, just a plain simple email. If not, make sure you try with as little code as possible, i.e. the code in my previous post.
  2. If the answer to #1 is no, does mail() return true or false? (Otherwise ignore).
  3. If the answer to #1 is yes, try the attachment sending code again and tell use whether mail() returns true or false. (Otherwise ignore).

Share this post


Link to post
Share on other sites
  • 0

Posted

yes I can receive an email from my original php code.

no I cannot receive an email using the code that you provided.

Share this post


Link to post
Share on other sites
  • 0

Posted

yes I can receive an email from my original php code.

no I cannot receive an email using the code that you provided.

 

Oh, well that's odd. Did my code print a success message but you never received it, or did it print a fail message because the mail delivery software rejected it?

Try my code with the from address set to what you used in your own code... Perhaps there's a restriction in place on what that domain can be?

 

We do need to figure out what's causing this problem, but since you can indeed receive an email with some code, we know that you've got a fully functional and complete setup for mail delivery available to you; i.e. you're not trying to do something like use the php mail function on a server without mail delivery software, and we know that the path between the mail delivery software on your server and your inbox is working. So it's helpful to know that.

Share this post


Link to post
Share on other sites
  • 0

Posted

Hi,

While I am sure it is possible, generally you will need to "stream" the data to a file storage for the email server to pull from. The file needs to be intact before sending through the email system.

*good luck*

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.