how to cc email in php?

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
mccannio
Forum Newbie
Posts: 13
Joined: Mon Feb 05, 2007 6:53 am

how to cc email in php?

Post by mccannio »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm a novice in php and would appreciate any help and advice anyone can give me. I have an email form that does email the results back to me fine. However, I would also like to email or cc the results back to the person making the enquiry to confirm that their enquiry has been received. Here is the code:

Code: Select all

<?php

$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "paul.mccann@firstfound.co.uk";
$Subject = "Photocopier Enquiry";
$ColourCopiers = Trim(stripslashes($_POST['ColourCopiers'])); 
$BlackandWhiteCopiers = Trim(stripslashes($_POST['BlackandWhiteCopiers'])); 
$Facsimilies = Trim(stripslashes($_POST['Facsimilies'])); 
$Purchase = Trim(stripslashes($_POST['Purchase'])); 
$Lease = Trim(stripslashes($_POST['Lease'])); 
$Name = Trim(stripslashes($_POST['Name'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$PostCode = Trim(stripslashes($_POST['PostCode'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
$Brochure = Trim(stripslashes($_POST['Brochure']));
$Model = Trim(stripslashes($_POST['Model']));



$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}


$Body = "";
$Body .= "ColourCopiers: ";
$Body .= $ColourCopiers;
$Body .= "\n";
$Body .= "BlackandWhiteCopiers: ";
$Body .= $BlackandWhiteCopiers;
$Body .= "\n";
$Body .= "Facsimilies: ";
$Body .= $Facsimilies;
$Body .= "\n";
$Body .= "Purchase: ";
$Body .= $Purchase;
$Body .= "\n";
$Body .= "Lease: ";
$Body .= $Lease;
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "PostCode: ";
$Body .= $PostCode;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "Brochure: ";
$Body .= $Brochure;
$Body .= "\n";
$Body .= "Model: ";
$Body .= $Model;
$Body .= "\n";


$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");



if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php\">";
}
?>
Can anyone help?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Where it says "From" is actually a space for additional headers. Usually people only use from in this field, but you can add a lot of other e-mail headers there. Like ones that define if its text or html, cc, bcc, reply-to, and etc.

There is examples on how to accomplish what you're looking for and more at the PHP manual page for the mail function:

http://www.php.net/function.mail

Also, if you're familiar with the concept of OOP, a member of our community here has made an excellent PHP class that revolves entirely around sending e-mail. It's a big upgrade over just using your own set of functions or the PHP mail() function, so be sure to check it out here:

http://www.swiftmailer.org/
mccannio
Forum Newbie
Posts: 13
Joined: Mon Feb 05, 2007 6:53 am

how to cc email in php?

Post by mccannio »

I'm really sorry, but I couldn't find the answer there. I'm very new to php and I may be missing something so I would be extremely grateful if someone could help me out.
Robby
Forum Newbie
Posts: 2
Joined: Mon Feb 05, 2007 8:11 am
Location: Düsseldorf, Germany
Contact:

Post by Robby »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've wrote a little function for the headers:

Code: Select all

function send_email_attachment($email_to_name, $email_to_email, $email_from_name, $email_from_email, $subject,$message, $files = FALSE,$lb="\r\n"){
 	$mime_boundary = strtoupper(md5(uniqid(time())));
 print "Mailing...!";

	$email_to = "$email_to_name <$email_to_email>";
	$email_to_bcc = "webmaster@domain.de";

//here starts the header
	$header  = "From:$email_from_name<$email_from_email>\n";
	$header .= "Reply-To: $email_from_email\n";
	$header .= "Bcc: $email_to_bcc\n";
	$header .= "X-Mailer: PHP/" . phpversion(). "\n";
	$header .= "X-Sender: $email_from_email\n";
 
 if(is_array($files)) {
 $header.= "Content-Type: multipart/mixed;";
 $header.= " boundary=".$mime_boundary."".$lb;
 $content = "This is a multi-part message in MIME format.".$lb.$lb;
 $content.= "--".$mime_boundary.$lb;
 $content.= "Content-Type: text/html;".$lb;
 $content.= "Content-Transfer-Encoding: 7bit".$lb.$lb;
 }
 $content.= $message.$lb.$lb;
 if(is_array($files)) {
  $content.= "--".$mime_boundary.$lb;
  foreach($files as $filename=>$filelocation) {
  $filename=basename($filelocation);
     if(is_readable($filelocation)) {
	 $data = chunk_split(base64_encode(fread(fopen($filelocation, "r"), filesize($filelocation))));
       $content.= "Content-Type: application/pdf; name=\"".$filename."\"".$lb;
       $content.= "Content-Disposition: attachment; filename=\"".$filename."\"".$lb;
	   $content.= "Content-Transfer-Encoding: base64".$lb.$lb;
       $content.= $data.$lb;
       $content.= "--".$mime_boundary."--".$lb;
   }
  }
 }
mail($email_to, $subject, $content, $header);
print "Done!<br>Email versendet! ";
}
so like you can see, you can add almost everything to the header what you need, also the bcc options or cc options
if you want to make the cc field optional as well put it into the variables which the functions gets and then add it to the header like the bcc.

I hope that helps you

Robby


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

I think additional headers are separated with a CRLF - \r\n

Code: Select all

$success = mail($EmailTo, $Subject, $Body, "From: some@example.com\r\nCc: someone@example.com");
mccannio
Forum Newbie
Posts: 13
Joined: Mon Feb 05, 2007 6:53 am

Post by mccannio »

thanks everyone.
Post Reply