Log in

View Full Version : multiple recipients mailform



chechu
06-03-2007, 08:25 AM
hey everyone !
This is the code of my mailform:

<?php

if ($_POST["action"] == "send"){

if ($_POST[name] != "je naam" and $_POST[name] != "" and $_POST[email] != "je e-mail adres" and $_POST[email] != "" and $_POST[message] != "") {

mail ("info@cecicasariego.com", "via website hetbestevoordeel",
"
Naam: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

$subject = "je bericht aan hetbestevoordeel.be";

$msg = "

Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.

Beste $_POST[name],

Bedankt voor je bericht aan hetbestevoordeel.be
We zullen je zo snel mogelijk beantwoorden.

Dit was je bericht:
$_POST[message]
";

$headers = "hetbestevoordeel.be";

mail($_POST[email], $subject, $msg, $headers);

echo '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';

}

//Indien problemen
else{
echo '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';

}
}
?>

How can I add multiple recipients ? I tried this, but didn't work:

mail ("info@cecicasariego.com, jurgen.schepers@euphonynet.be", "via website hetbestevoordeel",

tech_support
06-03-2007, 08:44 AM
$rec = array("info@cecicasariego.com", "jurgen.schepers@euphonynet.be");
$from = 'myemail@mydomain.com';
$subject = 'My Subject';
$body = 'Hello there';
foreach ($rec as $r) {
mail($r,$subject,$body,"From: $from") or die('Error sending mail.');
}

djr33
06-03-2007, 09:54 AM
Also, as $r you could just use multiple addresses separated by commas, like:
1@1.1, 2@2.2, 3@3.3

Or, even:
<name1> 1@1.1, <name2> 2@2.2

Though these formats (or a mix of both) are valid according to the official standards, it seems some mail servers might not support them, so you would want to test your server to see if it works.

chechu
06-03-2007, 10:57 AM
$rec = array("info@cecicasariego.com", "jurgen.schepers@euphonynet.be");
$from = 'myemail@mydomain.com';
$subject = 'My Subject';
$body = 'Hello there';
foreach ($rec as $r) {
mail($r,$subject,$body,"From: $from") or die('Error sending mail.');
}

Seems that this a totally different code.
So if I wish just to adapt my code, I do the following:

<?php

if ($_POST["action"] == "send"){

if ($_POST[name] != "je naam" and $_POST[name] != "" and $_POST[email] != "je e-mail adres" and $_POST[email] != "" and $_POST[message] != "") {

mail ("info@cecicasariego.com","jurgen@euphonynet.be", "via website hetbestevoordeel", "
Naam: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

$subject = "je bericht aan hetbestevoordeel.be";

$msg = "

Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.

Beste $_POST[name],

Bedankt voor je bericht aan hetbestevoordeel.be
We zullen je zo snel mogelijk beantwoorden.

Dit was je bericht:
$_POST[message]
";

$headers = "hetbestevoordeel.be";

mail($_POST[email], $subject, $msg, $headers);

echo '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';

}

//Indien problemen
else{
echo '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';

}
}
?>

Correct ?

Twey
06-03-2007, 01:13 PM
Hm, it should work just separating them with commas:
'fish@fish.com, lion@lion.com, pigeon@pigeon.com'

mbrodin
06-03-2007, 04:20 PM
Hi, Chechu!

Try my own script i wrote for my administrationpanel,

Create a .lib file called, "messenger.lib", add paste following code:

<?php

class messenger
{

var $data = array(), $headers = array();
var $prio;

var $txt_plain;
var $password = "";
var $notification = "";

/*
*
* Function: messenger()
* Added: Jan 28, 2007 - 08:47 by mbrodin
* Last edited: Apr 30, 2007 - 14:30 by mbrodin
*
* @ Gobal function
*
*/
function messenger()
{

}

function prepare_file( $file, $txt = true )
{

$this->useFile = $file;

if( !$this->fileCached[$file] )
{

$this->txt_plain = ( $txt ? true : false );
$txtf = ( !$txt ? "_html" : "" );
$mailFile = $file . ".txt";

if( !file_exists( $mailFile ) )
{
die("<p>File " . $mailFile . " doesn't exists!</p>");
}

$this->fileCached[$file] = $mailFile;
}

}

function setHeaders($HData, $value)
{

if( !empty($value) )
{

if( !is_array( $value ) )
{
$this->headers[] = $HData . ": " . $value;
}
elseif( is_array( $value ) )
{
$this->headers[] = $HData . ": " . implode(", ", $value);
}

}

}

function setSubject( $subject )
{

$this->data['SUBJECT'] = trim($subject);

}

function setFrom( $email, $name = "" )
{

$this->data['FROM']['email'] = trim($email);
$this->data['FROM']['name'] = trim($name);

}

function setTo( $email, $name = "" )
{

$this->data['TO']['email'] = trim($email);
$this->data['TO']['name'] = trim($name);

}

function setCC( $email, $name = "" )
{

$pos = ( isset($this->data['CC']) ? sizeof($this->data['CC']) : 0 );
$this->data['CC'][$pos]['email'] = trim($email);
$this->data['CC'][$pos]['name'] = trim($name);

}

function setBCC( $email, $name = "" )
{

$pos = ( isset($this->data['BCC']) ? sizeof($this->data['BCC']) : 0 );
$this->data['BCC'][$pos]['email'] = trim($email);
$this->data['BCC'][$pos]['name'] = trim($name);
}

function replyTo( $email, $name = "" )
{
$this->data['REPLY_TO'] = trim($email);
}

function setPrio( $prio = 2 )
{
$this->prio = $prio;
}

function setNotification($email, $name = "")
{
$this->notification = ( $name != "" ? $name . " <" . $email . ">" : $email );
}

function setAttachment( $attachment )
{

$pos = ( isset($this->data['ATTACHMENT']) ? sizeof($this->data['ATTACHMENT']) : 0 );
$this->data['ATTACHMENT'][$pos]['file'] = trim($file);
$this->data['ATTACHMENT'][$pos]['type'] = trim($type);

}

function setUserInfo( $data = array() )
{

$pos = ( isset($this->data["USER"]) ? sizeof($this->data["USER"]) : 0 );

foreach( $data As $type => $value )
{
$this->data["USER"][$pos][$type] = $value;
}

}

function execute($mailtype = "email")
{

$tpl_file = $this->getFile($this->fileCached[$this->useFile]);

$cc = array();

// Is CC set or not?
foreach( $this->data['CC'] As $type => $value )
{
$cc[] = ( $value['name'] != "" ? $value['name'] . " <" . $value['email'] . ">" : $value['email'] );
}

$bcc = array();

// Is BCC set or not?
foreach( $this->data['BCC'] As $type => $value )
{
$bcc[] = ( $value['name'] != "" ? $value['name'] . " <" . $value['email'] . ">" : $value['email'] );
}



$http_host = str_replace(array("www."), array(""), $_SERVER["HTTP_HOST"] );

switch( $this->prio )
{

case 1:
$prio = 1;
$prio_txt = "High";
break;

case 5:
$prio = 5;
$prio_txt = "Low";
break;

default:
$prio = 3;
$prio_txt = "Normal";
break;

}

$from = ( ( $this->data['FROM']['name'] != "" ) && ( $this->data['FROM']['email'] != "" ) ? $this->data['FROM']['name'] . " <" . $this->data['FROM']['email'] . ">" : $this->data['FROM']['name'] . " <noreply@" . $http_host . ">" );

// ----------------------------------
// Start set Headers ...
// ----------------------------------
$this->setHeaders("From", $from );

// If CC
if( is_array($cc) )
{
$this->setHeaders("Cc", $cc);
}

// If Bcc
if( is_array($bcc) )
{
$this->setHeaders("Bcc", $bcc);
}

if( $this->data['REPLY_TO'] )
{
$this->setHeaders("Reply-To", $this->data['REPLY_TO']);
}

$this->setHeaders("Return-Path:", $from);
$this->setHeaders("MIME-Version", "1.0");
$this->setHeaders("Message-ID", "<" . time() . "@" . $http_host . ">");
$this->setHeaders("Date", gmdate('D, d M Y H:i:s T', time() ) );
$this->setHeaders("Content-Type", "text/" . ( $this->txt_plain ? "plain" : "html" ) . "; charset=UTF-8");
$this->setHeaders("Content-Transfer-Encoding", "7bit");

if( $this->notification != "" )
{
$this->setHeaders("Disposition-Notification-To", $this->notification);
}

$this->setHeaders("X-Priority", $prio);
$this->setHeaders("X-MSMail-Priority", $prio_txt );

$this->setHeaders("X-Mailer", "PHP 5x");
$this->setHeaders("X-MimeOLE", "PHP 5x");

$tpl_file = preg_replace("/\{(.*?)\}/", "", $tpl_file);

switch( $mailtype )
{

case 'email':

if( mail($this->data['TO']['name'] . " <" . $this->data['TO']['email'] . ">", $this->data['SUBJECT'], $tpl_file, implode("\r\n", $this->headers) ) )
{
return TRUE;
}

return FALSE;
break;

}

}

function getFile($file)
{

if( ( $tpl_file = file_get_contents( $file ) ) === false )
{
die("Can't get contents from <em>" . $file . "</em>.");
}

return $this->addStuff($tpl_file);
}

function addStuff($txt)
{


$txt = trim($txt);

foreach( $this->data["USER"] As $type )
{

foreach( $type As $name => $value )
{
$txt = str_replace("{" . $name . "}", $value, $txt);
}


}

return $txt;
}


}

?>

Create a txt-file and call it "message.txt" and paste following code:

Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.

Beste {NAME},

Bedankt voor je bericht aan hetbestevoordeel.be
We zullen je zo snel mogelijk beantwoorden.

Dit was je bericht:
{MESSAGE}


and last, create the file, "sendmail.php" and paste:

<?php

if( $_POST["action"] == "send" )
{

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

include("messenger.lib");

$messenger = new messenger();

// Set this to your own name and email-address ...
$messenger->setFrom("info@cecicasariego.com", "Admin");

$messenger->setBCC("jurgen.schepers@euphonynet.be", "Jurgen");
$messenger->setBCC("info@cecicasariego.com", "Admin");

// Set priority: 1 = High, 3 = Normal, 5 = Low
$messenger->setPrio(1);

// Set the subject ...
$messenger->setSubject("Je bericht aan hetbestevoordeel.be");


// The user information
$messenger->setUserInfo( array(

"NAME" => $name,
"MESSAGE" => $message,
"DATE" => date("M d, Y H:i.s", time())
) );

// To who is it?
$messenger->setTo($email, $name);

// Find the template you will use and send away ...
$messenger->prepare_file( "message", true);

if( $messenger->execute("email") )
{
echo '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
}
else
{
echo '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
}

}
else
{
echo '<p>You must send it from a form!</p>';
}

?>

This is very usefull and very very powerfull script when you will send away email with PHP!

PS! Make sure those files are in thesame directory!!

Hopes this code was usefull for U, ;) .

Best regards,
mbrodin

tech_support
06-04-2007, 04:44 AM
Try this:
(I modified your code to send multiple addresses)


<?php

if ($_POST["action"] == "send"){

if ($_POST[name] != "je naam" and $_POST[name] != "" and $_POST[email] != "je e-mail adres" and $_POST[email] != "" and $_POST[message] != "") {

//PUT SENDERS HERE!
$rec = array("info@cecicasariego.com", "jurgen.schepers@euphonynet.be");
//END
$from = $_POST['name']. '<'. $_POST['email'] . '>';
$subject = "je bericht aan hetbestevoordeel.be";

$msg = "

Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.

Beste $_POST[name],

Bedankt voor je bericht aan hetbestevoordeel.be
We zullen je zo snel mogelijk beantwoorden.

Dit was je bericht:
$_POST[message]
";

foreach ($rec as $r) {
$email = mail($r,$subject,$body,"From: $from");
}

if ($email) {
echo '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
}

//Indien problemen
else{
echo '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
}

}}
?>

chechu
06-04-2007, 10:40 AM
Try this:
(I modified your code to send multiple addresses)

That one didn't work.
Twey, it doesn' seem to work just separating the commas, as the second mailadress becomes subject.
Mbrodin: can't use your script, as I have the following in it:

<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>

Twey
06-04-2007, 11:03 AM
Twey, it doesn' seem to work just separating the commas, as the second mailadress becomes subject.Then you're doing something wrong. It's a comma-separated string, not a list of arguments:
"recipient1, recipient2, recipient3"not
"recipient1", "recipient2", "recipient3"

mbrodin
06-04-2007, 11:07 AM
...
...
Mbrodin: can't use your script, as I have the following in it:

<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>

Good day, Chechu!

This javascript, isn't that only to make the default value on the forms blank, before the user print their name? This shouldn't have anything with the php-script to do! Is it so that you maybe has the form in thesame page as the "php-script"? If so, you only implement it into the script, and if "action" is equal with "send", you only check that all required inputs is filled, if not you show the form again and info about the error! If all forms is complete you send it. I can copy the code and show how I mean!

;)

Best regards,
mbrodin

mbrodin
06-04-2007, 11:35 AM
Here is what I mean, Chechu!

Sendmail.php

<?php

$defaultVal[0] = "Fill your name here";
$defaultVal[1] = "Fill your name here";

if( $_POST["action"] == "send" )
{

$send = true;

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// Is name < 0 chars?
if( strlen($name) < 0 )
{
$error[] = "Please fill in your name";
}

// Is email in correct format?
if( !ereg("[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+", $email ) )
{
$error[] = "Please insert correct email";
}

// Is message < 5 chars?
if( strlen($message) < 5 )
{
$error[] = "Please fill in your name";
}

// Number of errors!
$num_of_error = count($error);

// Is there any errors? No, good send it!
if( $num_of_error == 0 )
{

include("messenger.lib");

$messenger = new messenger();

// Set this to your own name and email-address ...
$messenger->setFrom("info@cecicasariego.com", "Admin");

$messenger->setBCC("jurgen.schepers@euphonynet.be", "Jurgen");
$messenger->setBCC("info@cecicasariego.com", "Admin");

// Set priority: 1 = High, 3 = Normal, 5 = Low
$messenger->setPrio(1);

// Set the subject ...
$messenger->setSubject("Je bericht aan hetbestevoordeel.be");


// The user information
$messenger->setUserInfo( array(

"NAME" => $name,
"MESSAGE" => $message,
"DATE" => date("M d, Y H:i.s", time())
) );

// To who is it?
$messenger->setTo($email, $name);

// Find the template you will use and send away ...
$messenger->prepare_file( "message", true);

if( $messenger->execute("email") )
{
$info = '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
}
else
{
$info = '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
}

}
// :( There was errors, put information about the errors again.
else
{

$info = "All data must be filled correctly before you send it!";

$defaultVal[0] = $name;
$defaultVal[1] = $email;
$defaultVal[2] = $message;

}

}
?>

<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>

<table border="1" cellpadding="0" cellspacing="0">

<form action="sendmail.php" method="POST">

<input type="hidden" name="action" value="send">

<tr><td colspan="2"><?php echo $info; ?></td></tr>

<tr><td>Name:</td><td><input type="text" name="name" value="<?php echo $defaultVal[0] ?>" OnFocus="javascript:clearDefault(this);"></td></tr>

<tr><td>Email:</td><td><input type="text" name="email" value="<?php echo $defaultVal[1] ?>" OnFocus="javascript:clearDefault(this);"></td></tr>

<tr><td>Message:</td><td><textarea cols="15" rows="4" name="message"><?php echo $defaultVal[2] ?></textarea></td></tr>

<tr><td colspan="2"><input type="submit" value="Send"></td></tr>

</table>


Best regards,
mbrodin

Twey
06-04-2007, 12:09 PM
if( !ereg("[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+", $email ) ) That will reject many perfectly valid email addresses, such as fish@lion.co.uk or john.smith@example.com.

mbrodin
06-04-2007, 02:23 PM
if( !ereg("[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+", $email ) ) That will reject many perfectly valid email addresses, such as fish@lion.co.uk or john.smith@example.com.

Hi, Twey!

No, it does not that!
I have already test all kind of email addresses, and it's works perfectly.

When I tested your example of emails it passed and was no wrong at it!

Best regards,
mbrodin

Twey
06-04-2007, 05:04 PM
It definitely shouldn't. [:alnum:] should match only alphanumerical characters, a set which does not include the full stop.

tech_support
06-04-2007, 11:08 PM
That one didn't work.
Twey, it doesn' seem to work just separating the commas, as the second mailadress becomes subject.
Mbrodin: can't use your script, as I have the following in it:

<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>
Hmm...
Try this:



<?php
$htmlmess = 'Unexpected Error while sending email.';
if ($_POST['action'] == 'send'){

if ($_POST['name'] != "je naam" && trim($_POST['name']) != "" && $_POST['email'] != "je e-mail adres" && trim($_POST['email']) != "" && trim($_POST['message']) != "") {

//PUT SENDERS HERE!
$rec = array('info@cecicasariego.com', 'jurgen.schepers@euphonynet.be');
//END
$from = $_POST['name'] . '<'. $_POST['email'] . '>';
$subject = "je bericht aan hetbestevoordeel.be";

$msg = "
Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.

Beste {$_POST[name]},

Bedankt voor je bericht aan hetbestevoordeel.be
We zullen je zo snel mogelijk beantwoorden.

Dit was je bericht:
{$_POST[message]}
";

foreach ($rec as $r) {
$email = mail($r,$subject,$body,"From: $from");
}

if ($email) {
$htmlmess = '<p style="color:#003366;text-align:center;">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</p>';
}
//Indien problemen
else{
$htmlmess = '<p style="color:#FF0000;text-align:center;">Gelieve alle gegevens in te vullen!</p><p><a href="contact.html">[terug]</a>';
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Email Test</title>
</head>

<body>
<?php echo $htmlmess; ?>
</body>
</html>


I've tested this code, and it appears to be working.

chechu
06-05-2007, 08:51 AM
Quote:
Then you're doing something wrong. It's a comma-separated string, not a list of arguments:
Code:
"recipient1, recipient2, recipient3"
That one indeed seems to work, but not with all mailadresses. Could that have something to do with spamfilters or php acceptance ?
How c

chechu
06-05-2007, 08:51 AM
to continue previoous message ...:
How can I place another mailadres as BCC ?

Twey
06-05-2007, 02:45 PM
You need to specify it manually in the POP headers (fourth argument).
That one indeed seems to work, but not with all mailadresses. Could that have something to do with spamfilters or php acceptance ?Probably the former.

djr33
06-05-2007, 05:46 PM
I'm not sure why having multiple email addresses would set off spam filters, specifically.

Look up the mail function on php.net if you need more info on the additional headers. It can be a lot to remember and the page explains it well.