I'm not a php coder, I know a little, but only basic. A friend of mine wrote the following bit of code for me, it is code to enable a contact form to be processed, usual stuff, email gets sent to the recipient and the user also gets a confirmation email. Well the client damn him, wants a pdf to be attached to the confirmation email.
It all seems to go through, until it comes to downloading and opening the pdf, it says that this:
"Acrobat could not open 'GPbrochure.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."
I tried this with a zip file too, and I got a similar response with regards to the file not being correctly decoded. Here is the code:
Code: Select all
<?php
if(isset($_POST['sendCom'])) {
$editFormAction = $_SERVER['PHP_SELF'];
/* define variables, arrays and patterns */
$error = array();
$data_array = array();
$required_fields = array(
"firstname",
"surname",
"job_title",
"surgery",
"address",
"town",
"email");
$pattern = '/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';
/* iterate through al;l the posted variables */
foreach($_POST as $key => $data) {
if(($data != '') || (!in_array($key, $required_fields))) {
$data_array[$key]=strip_tags($data);
} else {
$error[$key] = 'You did not complete the '. ucwords(str_replace("_", " ", $key)) .' field';
}
}
/* Run a quick injection check */
foreach($data_array as $data) {
if(stristr($data, "Content-Type:")) {
exit();
}
}
/* check email address */
if(isset($data_array['email'])) {
if(!preg_match($pattern, $data_array['email'])) {
$error['email_invalid'] = 'Invalid Email';
}
}
// Creates the variables for the email
$adminaddress = "nickandjo2002@yahoo.co.uk";
$siteaddress ="http://www.gpoutofhours.co.uk/";
$sitename = "GP Out Of Hours";
$personal_name = $data_array['title'] . '+' . $data_array['firstname'] . '+' .$data_array['surname'];
$personal_name_mail = $data_array['title'] . ' ' . $data_array['firstname'] . ' ' .$data_array['surname'];
if($_POST['title'] == 'Select a Title') {
$title = '';
}
$job_title = $data_array['job_title'];
$address = $data_array['address'];
$town = $data_array['town'];
$postcode = $data_array['postcode'];
$comments = $data_array['comments'];
$personal_name=str_replace("+", " ", $personal_name);
// If no errors, send email and redirect to acknowledgment page
if (empty($error)) {
$message = "
Title: $title
Firstname: $firstname
Surname: $surname
Job Title: $job_title
Address: $address
Town: $town
Postcode: $postcode
Tel: $tel
Email: $email
Comments: $comments
Requests:
$meeting
$free_trial
$call
$brochure_email
$brochure_post
Best time to call:
$day at $time.
";
//This sends a confirmation to your visitor
$fileatt = "http://www.gpoutofhours.co.uk/_pdf/"; // Path to the file
$fileatt_name = "GPbrochure.pdf"; // Filename that will be used for the file as the attachment
$email_message = "<p>Dear $personal_name,<br />
<br />
Many thanks for your enquiry today.<br />
<br />
Your message is being sent to me personally, and I will be back in touch with you to discuss
your enquiry at the very earliest opportunity.<br />
<br />
Attached to this message you will find our Out of Hours brochure, which will summarise our service in a
little more detail – I hope you find this useful.<br />
<br />
If you have any queries in the meantime, or wish to discuss any aspect of our service,
don’t hesitate to contact me on either freephone 0800 0830 630, or directly on 0797 415 9595.<br />
<br />
Kind regards<br />
<br />
Ali Doak<br />
<em>Operations Director</em><br /></p>
$sitename<br />
<p>2 Telford Court | Chester Gates Business Park | Chester | CH1 6LT<br />
t: 0800 0830 630 | f: 0800 111 6363 | m: 0797 415 9595 </p>
<p>www.gpoutofhours.co.uk</p>
<img src='http://www.gpoutofhours.co.uk/_images/GPstrapline.gif' />";
// Message that the email has in it
$email_to = $email; // Who the email is too
$email_subject = "Thank You for visiting ". $sitename ;
$headers = "From: ".$adminaddress;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$fileatt_type = "application/pdf"; // File Type
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$email_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);
// To add more files just copy the file section again, but make sure they are all one after the other! If they are not it will not work!
mail($email_to, $email_subject, $email_message, $headers);
mail ($adminaddress, "General Enquiry", $message, "FROM:". $adminaddress);
#mail ($email, "Thank You for visiting ". $sitename, $message2. '\n'. $message2a, $headers);
header('Location: thanks.php?name='. $personal_name);
exit();
/*
*/
}
}
?>Cheers
d11wtq | Added
Code: Select all
tags :)[/color]