Skip to main content
Participant
August 5, 2009
Question

How do i send an email with attachment in PHP?

  • August 5, 2009
  • 2 replies
  • 1332 views

Hello,

I have a form that gets people name, emial, etc. Right now its working correctly, what it does is it sends a email to a seperate email account of the info entered. But what i want to do now is have the person submitting their info to receive an automatic email with an attachment sent to the email they provided. I could really use your help.

Thank you.

Here is the PHP script:

  <?php
if(isset($_POST['email'])) {

$email_to = "sspromo@lvpaiute.com";
$email_subject = "Registration Form";


function died($error) {
  echo "We are very sorry, but there were error(s) found with the form your submitted. ";
  echo "These errors appear below.<br /><br />";
  echo $error."<br /><br />";
  echo "Please go back and fix these errors.<br /><br />";
  die();
}

if(!isset($_POST['first_name']) ||
  !isset($_POST['last_name']) ||
  !isset($_POST['email']) ||
  !isset($_POST['address']) ||
  !isset($_POST['city']) ||
  !isset($_POST['state']) ||
  !isset($_POST['zipcode']) ||
  !isset($_POST['comments'])) {
  died('We are sorry, but there appears to be a problem with the form your submitted.'); 
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$address = $_POST['address']; // required
$city = $_POST['city']; // required
$state = $_POST['state']; // required
$zipcode = $_POST['zipcode']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
   $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
   $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
   $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
   died($error_message);
  }
$email_message = "Form details below.\n\n";

function clean_string($string) {
   $bad = array("content-type","bcc:","to:","cc:","href");
   return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip Code: ".clean_string($zipcode)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$first_name."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@644699($email_to, $email_subject, $email_message, $headers); 
?>

This topic has been closed for replies.

2 replies

Participating Frequently
August 5, 2009

The wording of your post is confusing. Can you please clarify?

Do you want the visitor to attach and upload file which is then sent via email to one of your site's contacts?

Or, do you want the visitor that submitted the form to receive an email and attachment from you?

GrayFox32Author
Participant
August 5, 2009

I want the visitor to receive an email from me with an attached file, automatically after they submit the form.

Participating Frequently
August 5, 2009

Just search for 'php email attachement' and you'll find examples. Here's a few:

http://www.codewalkers.com/c/a/Email-Code/PHP-Email-Attachment-v1/

http://www.webcheatsheet.com/php/send_email_text_html_attachment.php

http://www.devshed.com/c/a/PHP/Handling-Attachments-in-MIME-Email-with-PHP/2/

DwFAQ
Participating Frequently
August 5, 2009

http://forums.adobe.com/message/2105478