Forum Moderators: coopster

Message Too Old, No Replies

Need help with PHP Code

formmail, form to email

         

amitganotra

4:16 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



I have this code and working perfectly fine. Now my problem is i need to send the mail to multiple recipients, TO, CC & BCC.

Please help me what code needs to be added and where.

Code
==========================

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "amit@kaff.in";
$email_subject = "Enquiry from Website...";

function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form your submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['name']) ¦¦
!isset($_POST['tel']) ¦¦
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form your submitted.');
}

$infoabout = $_POST['infoabout'];
$otherenq = $_POST['otherenq'];
$name = $_POST['name']; // required
$occu = $_POST['occu'];
$address = $_POST['address'];
$country = $_POST['country'];
$tel = $_POST['tel']; // required
$fax = $_POST['fax'];
$email_from = $_POST['email']; // required

$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "About Product: ".clean_string($infoabout)."\n";
$email_message .= "Other Enquiry: ".clean_string($otherenq)."\n";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Occupation: ".clean_string($occu)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Telephone #: ".clean_string($tel)."\n";
$email_message .= "Fax #: ".clean_string($fax)."\n";
$email_message .= "Email Address: ".clean_string($email)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here -->

<?
}
?>

<HTML>
<body>
<h1>Thankyou</h1>
<a href="http://www.kaff.in/">HOME</a>
</body>
</html>

CyBerAliEn

5:12 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Look near the bottom, the line that reads:

$headers = 'From: '.$email_from."\r\n".

Modify this line to read:


$headers = 'From: '.$email_from."\r\n".
'CC: '.$email_cc."\r\n".
'BCC: '.$email_bcc."\r\n".

Then... modify the top of the code to reflect the following modifications and alter as needed:


// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "amit@kaff.in";
$email_subject = "Enquiry from Website...";
$email_cc = "me@site.com,them@site.com";
$email_bcc = "them@other.com";

I have not tested your code to find out if it works or not. But these modifications should allow you to send both CC and BCC along with this message.

I would also probably add conditionals so that if 'cc' or 'bcc' are not set, it will not try to include them. (fyi)

Good luck.

amitganotra

5:32 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



thanks for reply but its not working. I wish if i could get a complete PHP code to send form data on multiple recipients.

CyBerAliEn

6:01 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Did it send back an error?

Or did you simply fail to receive the email as expected?

Temporarily, replace

@mail($email_to, $email_subject, $email_message, $headers); 

With...

$sent = mail($email_to, $email_subject, $email_message, $headers);
if ($sent) { echo "<p>email sending successful</p>"; }
else { echo "<p>email sending failed</p>"; }

To find out if PHP is successfully sending the email on its end.