Jump to content

[SOLVED] PHP Mail with attachment


marksie1988

Recommended Posts

Hi,

 

i am trying to get the following script to attach a file to it that is added from a mail form our website, i cant seem to get it to work i have tried various methods but i cant seem to get it to work.

 

its a job application form and the users need to attach a CV so if i could only allow certain file types that would be a bonus but not essential.

 


<?php

$to = "email@work.com";
$subject = "Sales Job Application";

# -=-=-=- PHP FORM VARIABLES
$name = $_POST["txtName"];
$add1 = $_POST["txtAddress1"];
$add2 = $_POST["txtAddress2"];
$pcode = $_POST["txtPostcode"];
$phone = $_POST["txtPhone"];
$mobile= $_POST["txtMobile"];
$sallary = $_POST["txtSalary"];
$AMS = $_POST["txtAMS"];
$salary = $_POST["txtSalary"];

$attachment = $_FILES['txtCv']['tmp_name'];
$attachment_name = $_FILES['txtCv']['name'];
if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
$fp = fopen($attachment, "rb"); //Open it
$data = fread($fp, filesize($attachment)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
}
# -=-=-=- CHECK EMAIL VALIDITY

function check_email_address($email) {
  // First, we check that there's one @ symbol, and that the lengths are right
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    header("Location: ../allabout.php?subSelection=Careers&form=sales&status=0");
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
     if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
       header("Location: ../allabout.php?subSelection=Careers&form=sales&status=0");
    }
  }  
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
         die ("Invalid email address");// Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
         header("Location: ../allabout.php?subSelection=Careers&form=sales&status=0");
      }
    }
  }
  return $email;
}
function escape_val($string) {
$string = str_replace(array('"',"<",">"), array(""","<",">"), $string);	
return $string;
}
$check_email = check_email_address($_REQUEST['from']); 


# -=-=-=- MIME BOUNDARY

$mime_boundary = "Mail Form".md5(time());

# -=-=-=- MAIL HEADERS



$headers = "From: $nme <$check_email>\n";
$headers .= "Reply-To: $nme<$check_email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";

# -=-=-=- TEXT EMAIL PART

	$fileatt = $_FILES['txtCv']['tmp_name']; // Path to the file 
	$fileatt_type = $_FILES['txtCv']['type']; // File Type 
	$fileatt_name = $_FILES['txtCv']['name']; // Filename that will be used for the file as the attachment 

	$file = fopen($fileatt,'rb'); 
	$data = fread($file,filesize($fileatt)); 
	fclose($file); 


	$data = chunk_split(base64_encode($data)); 

	$message .= "--{$mime_boundary}\n" . 
	"Content-Type: {$fileatt_type};\n" . 
	" name=\"{$fileatt_name}\"\n" . 
	//"Content-Disposition: attachment;\n" . 
	//" filename=\"{$fileatt_name}\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . 
	$data . "\n\n" . 
	"--{$mime_boundary}\n"; 
	unset($data);
	unset($file);
	unset($fileatt);
	unset($fileatt_type);
	unset($fileatt_name);

$message = "--$mime_boundary\n";
$message .= "Content-Type: text/plain; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";

$message .= "name: $name\n\n";
$message .= "Company: $company\n\n";
$message .= "Address: \n";
$message .= "$add1,\n";
$message .= "$add2,\n";
$message .= "$city,\n";

$message .= "$pcode,\n\n";
$message .= "Phone: $phone\n";
$message .= "Fax: $fax\n\n";
$message .= "Extra Info:<br />$einfo\n";

# -=-=-=- HTML EMAIL PART

$message .= "--$mime_boundary\n";
$message .= "Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";

$message .= "<html>\n";
$message .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:14px; color:#666666;\">\n";
$message .= "<b>Name:</b> $name<br />\n";
$message .= "<br />\n";
$message .= "<b>Company:</b> $company<br />\n";
$message .= "<b>Address:</b> <br />$add1, <br />$add2,<br />$city,<br />$pcode\n\n";
$message .= "<br /><br />\n";
$message .= "<b>Phone:</b> $phone<br /><b>Fax:</b> $fax\n\n";
$message .= "<b>Extra Info:</b><br />$einfo\n";
$message .= "</body>\n";
$message .= "</html>\n";


# -=-=-=- FINAL BOUNDARY

$message .= "--$mime_boundary--\n\n";



# -=-=-=- SEND MAIL
$mail_sent = @mail( $to, $subject, $message, $headers );
if($mail_sent) {
header("Location: ../allabout.php?subSelection=Careers&form=sales&status=1");
exit;
} 
else {
header("Location: ../allabout.php?subSelection=Careers&form=sales&status=0");
}
?>

Link to comment
Share on other sites

is there areason why you have put each line in its own

$message = "--$mime_boundary\n";

  i have written an email system in php before which in cluded html before.

 

you can just start the email wit the first

$message ="

  put all your content after the " and then when you have finished constructing the message finish the line the same as you have at the end of each message.

 

as for the cv part this could be done using a form to first upload the file and save it on the server and then use the if fille exists function in php to get the file from the server store it in a variable which would hold the absolute path the stored cv.  this could then be used inside a link so the file can be downloaded from the server once it is needed.

 

sorry if i have missed the point of your post.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.