PDA

View Full Version : php mail attachment



d_reaper86
08-22-2008, 10:52 AM
I have the following PHP mailform but need to add into it a function so that people can upload attachments. i have seen plenty of coding to have an attachment option but no idea how to intergrate it into this code.

ne help would be appreciated.


/*
* Set the following variables as required
*
*/

/* Who the form data gets emailed to */
$recipient = "info@jspsolicitors.co.uk";

/* Name of the person the form is emailed to */
$name = "Jenny";

/* Subject of the email */
$subject = "Enquiry for JSP";

/* Who the email is "from" */
$from = "webmaster@lincolnwebenterprises.co.uk";

/* Name of any mandatory fields (case sensitive) comma separated list */
$mandatory = "name,email,telephone";

/* Page to display if script called directly or if mandatory fields empty */
$try_again = "errorcontact.html";

/* Page to display if mail was sent ok */
$return_ok = "thank-you.html";

/* Page to display of there was a problem sending the email */
$return_bad = "oooops.html";

/* The text at the start of the email */
$msg = "Dear $name,\n\n".
"Someone completed the 'contact us' form on your website.\n".
"Here are the fields of the form:\n";

/* The text at the end of the email */
$thend = "\nRegards,\nLincoln Web Enterprises\n\n";

/* Comment this out to use PHP's error handling */
$old_error_handler = set_error_handler("myErrorHandler");


/**********************************************************
*
* Don't modify anything below this line,
* unless you know what you're doing ;)
*
**********************************************************/

if(empty($_POST)){ // Display HTML if called directly, removing variable holders

if(is_file($try_again)){
$buf=file_get_contents($try_again);
$buf=preg_replace('/\{.+\}/','',$buf);
echo $buf;
}else{
echo "<html><head><title>Error Message</title></head>\n\n";
echo "<body><center>Error: Script called directly but can't ";
echo "find page $try_again !!</center>\n\n";
echo "</body></html>";
}

}else{ // Otherwise process form variables

$err='';
$arr_mandatory = explode(',',$mandatory);
foreach($arr_mandatory as $val){ // Check mandatory variables
$ok=true;
if(isset($_POST[$val])){
if(empty($_POST[$val])){
$ok=false;
}
}else{
$ok=false;
}
if(!$ok){
$err.= "Please complete the '$val' field<br />";
}
}

if(empty($err)){ // Send email if everything ok so far

$msg.= "\n----------8< FORM DATA\n";
foreach($_POST as $key => $val){
$msg.= "$key : $val\n";
}
$msg.= "----------8< END OF FORM DATA\n";
$msg.= $thend;
$hdr = "From: $from";
if (mail($recipient,$subject,$msg,$hdr,"-f $from")) {
header("location:$return_ok");
}else{
header("location:$return_bad");
}

}else{ // Otherwise display error to user

if(is_file($try_again)){
$buf=file_get_contents($try_again);
$buf=str_replace('{error_message}',$err,$buf);
foreach($_POST as $key => $val){
$val=htmlspecialchars($val);
$pattern="{value_$key}";
$buf=str_replace($pattern,$val,$buf); // Re-populate fields
}
echo $buf;
}else{
echo "<html><head><title>Error Message</title></head>\n\n";
echo "<body><center>Error: Can't find page $try_again !!</center>\n\n";
echo "</body></html>";
}
}

}

return;

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "<b>ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;

case E_USER_WARNING:
echo "<b>WARNING</b> [$errno] $errstr<br />\n";
break;

case E_USER_NOTICE:
echo "<b>NOTICE</b> [$errno] $errstr<br />\n";
break;

default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}

/* Don't execute PHP internal error handler */
return true;
}

?>


Wrapped code in
tags.

Nile
08-22-2008, 10:02 PM
Check out PHPs Mail() Function (http://us.php.net/manual/en/function.mail.php) to look at, if you just look around there, you can find how to attach files. You can also look at some other tutorials, or search on google witch returns many results.