Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to send result from a form to many email addresses at the same time?

2 views
Skip to first unread message

Karl-Arne Gjersøyen

unread,
Sep 2, 2010, 10:52:18 AM9/2/10
to
Hello.
On an private webserver I shall have a form that send the content to 10
different email adresses at the same time.

The E-mail addresses is written in a textfile. My "program" open and
read this file and I try to store the result in $Data.

Then I use mail() in PHP and try to send the result by using Cc. (When
it work I shall us Bcc instead.)

Well. THe script send two emails. It is to my specified $to address and
one copy to the first email address that is written in the first line in
my textfile.

Can somebody try to help me find a solution on this problem?

Thanks for all advice.
Kind Regards,
Karl-Arne.

Here is the form and the script...

<form action="index.php" method="post">
<p>Name:<br /><input type="text" name="name" size="50" maxlength="50" /></p>
<p>E-mail:<br /><input type="text" name="email" size="50" maxlength="80"
/> </p>
<p>Subject:<br /><input type="text" name="subject" size="50"
maxlength="50" /></p>
<p>Message:<br /><textarea cols="50" rows="10"
name="message"></textarea></p>
<p><input type="reset" value="reset"/> <input type="submit"
name="prayer_request" value="Send" /></p>
</form>

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

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

// Read the textfile and get all addresses...
$myFile = "ma.txt";
$handle = fopen($myFile, 'r');
$Data = fread($handle, 10000);
fclose($handle);
$Data = str_replace("\n","", $Data);

// Send it
$to = "Karl@localhost, ";
$from = "From: $mail";
$subject = "$subject";
$body = "From: $name\nEmail: $from\nSubject:
$subject\nMessage:\n$message";
$header = "$from\n";
$header .= "Cc: $Data";
mail($to, $subject, $body, $header);

echo "<p>Message is sent</p>";


}


?>

]-[oRus

unread,
Sep 2, 2010, 11:15:54 AM9/2/10
to
Op 02-09-2010 16:52, Karl-Arne Gjersøyen schreef:

I guess you have to replace \n with , instead of nothing. Multiple
addresses have to be separated with a comma.

--
Vriendelijke groeten,

]-[oRus
www.smallsystemservice.nl

Jerry Stuckle

unread,
Sep 2, 2010, 11:18:17 AM9/2/10
to

Look at your CC: list and see if it is what you expect - format,
separators, etc. If so, then look at your MTA's logs to see what it
doesn't like about the list.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Karl-Arne Gjersøyen

unread,
Sep 2, 2010, 11:21:31 AM9/2/10
to
]-[oRus skrev:
I have try that. It still not work...

Karl

MG

unread,
Sep 2, 2010, 12:24:14 PM9/2/10
to
>> I guess you have to replace \n with , instead of nothing. Multiple
>> addresses have to be separated with a comma.
>>
> I have try that. It still not work...
>
> Karl

My system separates addresses with a ; and not a ,
Perhaps you can try that.

Something else you should do is to google Email Injection. Your code is very
vulnerable to attack.

MG


Karl-Arne Gjersøyen

unread,
Sep 2, 2010, 12:32:16 PM9/2/10
to
MG skrev:
My code is just at the very beginning. First I try to figure out how it
shall work, then I add security code....

I have done some minor changes.. Look here...


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

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

// Hent inn adresser fra tekstfil

$myFile = "ma.txt";
$handle = fopen($myFile, 'r');
$Data = fread($handle, 10000);
fclose($handle);

// Replace \n with ,
$Data = str_replace("\n",",", $Data);



// Send it
$to = "Karl@localhost, ";
$from = "From: $mail";
$subject = "$subject";
$body = "From: $name\nEmail: $from\nSubject:
$subject\nMessage:\n$message";
$header = "$from\n";

// Example taken from PHP Manual

$arr = array("$Data");
reset($arr);
while (list(, $value) = each($arr)) {

// When echo this, it looks OK out....
echo $header .= "Cc: $value\n";
}
//$header .= "Karl@localhost";
mail($to, $subject, $body, $header);

echo "<p>Message is sent</p>";


}


?>

BUT, still is it only one e-mail address in Cc field... Only two copies
of the message get out to the members...

I have look in logs and can't fine anything that explain this....

Karl

Michael Fesser

unread,
Sep 2, 2010, 12:46:14 PM9/2/10
to
.oO(Karl-Arne Gjersøyen)

>Hello.
>On an private webserver I shall have a form that send the content to 10
>different email adresses at the same time.
>
>The E-mail addresses is written in a textfile. My "program" open and
>read this file and I try to store the result in $Data.
>
>Then I use mail() in PHP and try to send the result by using Cc. (When
>it work I shall us Bcc instead.)

>[…]

It might be easier to use a class like PHPMailer instead. This would
also make the code more secure and not vulnerable for header injection.

Micha

Karl-Arne Gjersøyen

unread,
Sep 2, 2010, 12:48:31 PM9/2/10
to
Michael Fesser skrev:

Yes, ofcourse. But I'm in a learning situation.. I like to figure out
how to do things..

Thanks anyway for your tip!

Karl

Jerry Stuckle

unread,
Sep 2, 2010, 2:51:37 PM9/2/10
to

All this does is create an array with one element - the contents of $Data.

> reset($arr);
> while (list(, $value) = each($arr)) {
>
> // When echo this, it looks OK out....
> echo $header .= "Cc: $value\n";
> }

What do you get? We don't know what's in your file...

> //$header .= "Karl@localhost";
> mail($to, $subject, $body, $header);
>
>
> echo "<p>Message is sent</p>";
>
>
> }
>
>
> ?>
>
> BUT, still is it only one e-mail address in Cc field... Only two copies
> of the message get out to the members...
>
> I have look in logs and can't fine anything that explain this....
>
> Karl


Again, what does the header itself look like? And does your MTA show
any errors?

You can keep making stabs in the dark and maybe find the problem - or
you can troubleshoot the problem. I find the latter to be much faster.

Karl-Arne Gjersøyen

unread,
Sep 3, 2010, 3:17:17 AM9/3/10
to
Jerry Stuckle skrev:


My file ma.txt has this content:
Karl@localhost
Heidi@localhost
Mi-Rakel@localhost
Bolla@localhost

When I echo $headers I get this result on screen:
From: karl...@gmail.com Cc: Karl@localhost ,Heidi@localhost
,Mi-Rakel@localhost ,Bolla@localhost

Message is sent

But only two message appear in my inbox. It is to the address written in
$to: field and the very first address written in Cc: field.

>
>> //$header .= "Karl@localhost";
>> mail($to, $subject, $body, $header);
>>
>>
>> echo "<p>Message is sent</p>";
>>
>>
>> }
>>
>>
>> ?>
>>
>> BUT, still is it only one e-mail address in Cc field... Only two copies
>> of the message get out to the members...
>>
>> I have look in logs and can't fine anything that explain this....
>>
>> Karl
>
>
> Again, what does the header itself look like? And does your MTA show
> any errors?

No, my MTA did not show any errors...

The headers look like this:

From - Fri Sep 03 09:13:57 2010
X-Account-Key: account5
X-UIDL: S0K0VD5.CNMA9ADF1
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:

Received: from spooler by localhost (Mercury/32 v4.62); 3 Sep 2010
09:13:53 +0200
X-Envelope-To: <Karl@localhost>
Return-path: <karl...@gmail.com >
Received: from 20-01-2009 (127.0.0.1) by localhost (Mercury/32 v4.62) ID
MG000002;
3 Sep 2010 09:13:44 +0200
Date: Fri, 03 Sep 2010 09:13:44 +0200
Subject: Hello
To: Karl@localhost,
From: karl...@gmail.com
Cc: Karl@localhost
,Heidi@localhost
,Mi-Rakel@localhost
,Bolla@localhost

I don't know why those other addresses in Cc: not is on the same line. I
guess that is the problem?

Thanks again for your help!

Karl

Karl-Arne Gjersøyen

unread,
Sep 3, 2010, 3:23:26 AM9/3/10
to
Karl-Arne Gjersøyen skrev:

I Found the solution. I had to add \r in $Data = str_replace("\r\n",",",
$Data). Now it works very well.

Karl

0 new messages