Mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NickToye
Forum Newbie
Posts: 10
Joined: Thu May 11, 2006 7:14 am

Mail()

Post by NickToye »

Hi,

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();
/*
  */
  }
  
   }
?>
Anyone know what I need to do, as its becoming a massive thorn in my side.

Cheers

d11wtq | Added

Code: Select all

 tags :)[/color]
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Give phpMailer a try. It can handle attachments and much else.
NickToye
Forum Newbie
Posts: 10
Joined: Thu May 11, 2006 7:14 am

Post by NickToye »

Cheers,

I'll try that. Bare in mind that I am a total newbie and not php minded at all. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Here's my blatant plug :P If phpMailer doesn't work for you, you could guinea pig Swift Mailer.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That plug just seemed so natural. And the timing was impecable (did I spell that right?).
Subyne Simean
Forum Newbie
Posts: 19
Joined: Tue May 09, 2006 1:59 pm

Post by Subyne Simean »

At least he can get his froms working. grrr

not found a php server yet, just a busy busy man
NickToye
Forum Newbie
Posts: 10
Joined: Thu May 11, 2006 7:14 am

Post by NickToye »

Hi can anyone see where the problem is here:

Code: Select all

<?php

/*
 Run a "find" across this file for all the comments I've started with " /*** "
 */

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'])) /*** CHECK YOUR FRIEND'S $pattern!! IT'S GOT A BIG LOOPHOLE! ***/
                {
                        $error['email_invalid'] = 'Invalid Email';
                }
         }
 
        if($_POST['title'] == 'Select a Title')
        {
                $title = '';
        }
       
        // Creates the variables for the email
        $adminaddress = "info@nicktoye.co.uk";
        $siteaddress ="http://www.gpoutofhours.co.uk/";
        $sitename = "GP Out Of Hours";
        $personal_name = $data_array['title'] . '+' . $data_array['firstname'] . '+' .$data_array['surname'];

        if (!empty($data_array['title']))
        {
                $personal_name_mail = $data_array['title'] . ' ' .$data_array['surname'];
        }
        else
        {
                $personal_name_mail = $data_array['firstname'] . ' ' . $data_array['surname'];
        }
       
       
        $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))
        {
                /* Multi-line string against margin */
$message .= "<style type='text/css'>";
$message .= "BODY,P { font-family: Trebuchet; font-size: 10px; }";
$message .= "</style>"; 
$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.


";
                /* End string */

                //This sends a confirmation to your visitor
                $fileatt = "_pdf/GPbrochure.pdf"; // Path to the file  /*** WHERE IS THIS FILE??? ***/                 
                $fileatt_name = "GPbrochure.pdf"; // Filename that will be used for the file as the attachment

                /* Multi-line string against margin */
$email_message = "<p>Dear $personal_name_mail,<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' />";
                /* End string */

                // Message that the email has in it 
                $email_to = $email; // Who the email is too
                $email_subject = "Thank You for visiting ". $sitename;

                $fileatt_type = "application/pdf"; // File Type 

                $data = file_get_contents($fileatt);
               
                /*** YOU NEED TO EXTRACT THESE FILES FROM THE .zip AND RENAME THE Swift-0.0.1 FOLDER TO JUST "Swift" ***/
                require('Swift/Swift.php');
                require('Swift/Swift/Swift_Sendmail_Connection.php');

				$conn =& new Swift_Sendmail_Connection;
                /*** CHANGE your.smtp.server TO YOUR REAL SMTP SERVER (e.g. smtp.yourisp.com) ***/
                $swift =& new Swift($conn, $_SERVER['SERVER_NAME']);
               
                if (!$swift->hasFailed())
                {
					/*** THIS IS THE MAIL GOING TO THE CLIENT ***/
					$swift->addPart($email_message, 'text/html');
					$swift->addAttachment($data, $fileatt_name, $fileatt_type);
					$swift->send($email_to, $adminaddress, $email_subject);
					//Flushes here
					
					/*** THIS IS A BASIC MAIL GOING TO THE ADMIN ***/
					$swift->send($adminaddress, $adminaddress, "General Enquiry", $message);
					
					$swift->close();
				}
                else echo 'Something went wrong! Errors: '.print_r($swift->errors, 1).' Log: '.print_r($swift->transactions, 1);
                /*** REMOVE THE ABOVE ELSE STATEMENT BEFORE USING IN PRODUCTION ***/
				$swift->close();

                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();
                /*
                */
        }
 
}

?>
The email is sending fine, however the attachment is not showing up.

Also is there anyway I can specify the font and size of the email?

Cheers
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

magic_quotes_gpc is turned on. Your attachments are being received full of backslashes.

Code: Select all

if (ini_get('magic_quotes_gpc')) $data = stripslashes($data);

//Magic Quotes should die!!!!
NickToye
Forum Newbie
Posts: 10
Joined: Thu May 11, 2006 7:14 am

Post by NickToye »

can we apply css styles within the php code?
Post Reply