Let's say I have a form with 'file' type input control on it among other input controls.
My PHP emails the submitted form data somewhere.
As far as I know, in order to attach an uploaded from the form file to the email, I have to save it first in the temporary directory on the server and then attach it to the email from there.
Is this the only way to send a file as an email attachment with the rest of the form data?
Is there any better ways to do that?

Thank you!

Recommended Answers

All 6 Replies

As far as I know, uploading it somewhere is the only method

As far as I know, uploading it somewhere is the only method

Thank you!

As far as I know, uploading it somewhere is the only method

Actually, I've got a problem here.
I was able to accomplish the first part of the task - upload a file through my HTML form into a temporary directory that I created on the server.
Now, the examples of the code I found online that attaches a file to an email are all based on a specific file with a known name.
In my case, I am not going to know the exact file name I'm attaching, because it's a file uploaded by a user through the HTML form.
Do you know how to work around this situation?

Thank you!

You need to create a folder called "attachments" or stuff like that and upload it there with move_uploaded_file() function.
You can get it's name by entering $_FILES

I have similar stress that I will really appreciate fast response. I have a form as below and I need the action script in php.
<?php
$msg = "Message from Platform Form\n";
$msg .= "Aspirant's Background:\t$_POST[aspirant_background]\n";
$msg .= "Aspirant's choice:\t$_POST[aspirant_choice]\n";
$msg .= "Aspirant's Offer:\t$_POST[aspirant_offer]\n";
$msg .= "Aspirant's email:\t$_POST[aspirant_email]\n";
$msg .= "Aspirant's Photo:\t$_POST[aspirant_photo]\n";
$to = "info@helegrow.com";
$subject = "Platform Submission";
$mailheaders = "From: The site <> \n";
$mailheaders .= "Reply-To: $_POST[sender_email]\n\n";
mail($to, $subject, $msg, $mailheaders);
?>
The line I made bold and colored is the spot. I want that line to pick a picture from the use but I just can't do it.


Please I really need it.

You need to create a folder called "attachments" or stuff like that and upload it there with move_uploaded_file() function.
You can get it's name by entering $_FILES

So I created a folder called "uploads" and upload the files there with move_uploaded_file() function.
This part is fine.
Now I need to get the file from the "uploads" folder, attach it to am email and send it to a certain email address.
How do I do that?
Could you please give me an example code?
I tried something and it just didn't work.
Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.