Php Email Script Help with attachment

Hi everyone. I am here because I need some help. I have searched and searched for days and read so many articals on this but I admit at 61 I just can not get my head around it. So here I am asking you for your help. My Wife and I run an Internation Homestay and we have an online application form our students must fill out and submit to us for a Homestay Booking The form and the php mail script work fantastic and have so for several years. However now I need to add an attachment part to the form and email This attachment will be the students current photo in jpg, gif or png file format. The pgoto would be attached to the email application form. Here is what I have. This is the input for the attachment on the form:

<tr><td><p>Please send us a current Photo of you: </td><td>
<input type="file"send us you photo name="attachment" />

This part goes into my form.

Here is my current php mail script:


//--------------------------Set these paramaters--------------------------

$subject = 'New Homestay Application';                // Subject of email sent to you.
$emailadd = '';        // Your email address. This is where the form information will be sent.
$url = 'thankyou.html';               // Where to redirect after form is processed.
$req = '0';                                  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.

// --------------------------Do not edit below this line--------------------------
$text = "New Homestay Application:\
$space = ' ';
$line = '
foreach ($_POST as $key => $value)
	if ($req == '1')
		if ($value == '')
		{echo "$key is empty";die;}
	$j = strlen($key);
		if ($j >= 20)
	$j = 20 - $j;
		for ($i = 1; $i <= $j; $i++)
		{$space .= ' ';}
	$value = str_replace('\
', "$line", $value);
	$conc = "{$key}:$space{$value}$line";
	$text .= $conc;
	$space = ' ';
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

Please note that the application form is very long and I realy do not re-wright it all over again to accomodate this new part and I am hopping that someone here can help me with the script to make this work.

Kindest Regards
and thank you for your help.

Try using the PHPMailer class, this way you don’t need to re-invent the wheel.
Just google it.

Hi pagemaker69,

You don’t need to make too many changes to your actual form to allow uploads

  • Make sure you set the enctype attribute of the <form> tag to multipart/form-data:

    html <form enctype="multipart/form-data" action="youraction.php" method="POST">
  • Add a hidden field to specify the maximum file size (in bytes - below it’s set to 1mb) of the photo that users can attach. Note that this has to come before the file input tag:

    html <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

From PHP, you can access the uploaded file via the $_FILES global variable. You can find details about this in the PHP manual, here:

If you look at example 2 on that page, it shows you how to validate the uploaded file, which you can then attach to the email. Adding images to emails sent with PHP’s mail function can be tricky, so solidcodes suggestion about using PHPMailer is a good idea - you can see in this example how to send an email with an image attachment.

Hope that helps. Feel free to reply if you want more help with any particular step.

I am not re-inventing the wheel Just what to modify the script to send attachment is all. Not interested in installing new php mailer program on my server when what I have works just fine just need some help.

What you have works fine with sending the form, but you want to upload and attach an image to the email right? And unfortunately, it’s not just a case of changing a line or two of code.

If you really want to keep using PHP’s mail function directly, take a look here, and [URL=“”]here to get an idea of what’s involved. The nice thing about using a library like PHPMailer is that it hides all that complexity so you don’t have to deal with it.

Problem solved Thanks I rewrite the script to accomodate for atachment and made small ajustment to my form.