PHP Mail() Query
Thread Starter
Per Ardua ad Astraeus
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
PHP Mail() Query
Trying to use mail() to send to 2 recipients, as mail('[email protected],[email protected]', 'etc', 'etc') but it only ever sends to the first. Wot am I doing wrong please?

Joined: May 2004
Aviation Qualifications: CPL
Posts: 965
Likes: 46
From: Ташкент
<?php
require("class.phpmailer.php");
$mail= new PHPMailer();
$mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
$mail->Body = "shredded wheat sucks";
$mail->send();
?>
require("class.phpmailer.php");
$mail= new PHPMailer();
$mail->AddAddress("[email protected]");
$mail->AddAddress("[email protected]");
$mail->Body = "shredded wheat sucks";
$mail->send();
?>
Thread Starter
Per Ardua ad Astraeus
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
Thanks for reply, F8, but I have a form mail task and cannot see how to fit that in. This is where I am at:
<?php
if($_POST[submit])
{
$message = "Your inside leg:\r\n$_POST[field1]\r\n
Your etc etc;
Your etc etc;
'mail([email protected],[email protected], 'TITLE', $message,'From: email addy');
'echo ' Thank you - Message sent.';
'}
'else
'{
'?>
<form action="" method="post"><br>
<br>
Your inside leg:<br>
<input type="text" size="40" maxlength="40" textarea name="field1"><br><br>
Your etc etc
<input type="submit" name="submit" value="Submit Form">
</form>
Probably a few ;'s and <'s missing in the copying but it works to a single address and it is the multiple email addresses function I seek - or a major rewrite to your code :-(((
<?php
if($_POST[submit])
{
$message = "Your inside leg:\r\n$_POST[field1]\r\n
Your etc etc;
Your etc etc;
'mail([email protected],[email protected], 'TITLE', $message,'From: email addy');
'echo ' Thank you - Message sent.';
'}
'else
'{
'?>
<form action="" method="post"><br>
<br>
Your inside leg:<br>
<input type="text" size="40" maxlength="40" textarea name="field1"><br><br>
Your etc etc
<input type="submit" name="submit" value="Submit Form">
</form>
Probably a few ;'s and <'s missing in the copying but it works to a single address and it is the multiple email addresses function I seek - or a major rewrite to your code :-(((

Joined: May 2004
Aviation Qualifications: CPL
Posts: 965
Likes: 46
From: Ташкент
Sorry I am no "expert" on these things!
Did you look at: http://uk3.php.net/function.mail ??
It may help - anyhow good luck!
Did you look at: http://uk3.php.net/function.mail ??
It may help - anyhow good luck!
Thread Starter
Per Ardua ad Astraeus
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
Yep! It also says "[email protected], [email protected]" but that only seems to send to the first with me.
Its going to be one of those
answers - I know it......
Its going to be one of those
answers - I know it......

Joined: May 2004
Aviation Qualifications: CPL
Posts: 965
Likes: 46
From: Ташкент
why not do the following:
on the form to be posted:
<input type="submit" name="submit" value="myvalue">
and on the page it post's to....
$value = $_POST['myvalue']; or $value = $_GET['myvalue']; (cant remember which one you need)
and then the code I mentioned....
anyway something like this... I really am an amateur at coding but I did play around quite a bit a few years back!
on the form to be posted:
<input type="submit" name="submit" value="myvalue">
and on the page it post's to....
$value = $_POST['myvalue']; or $value = $_GET['myvalue']; (cant remember which one you need)
and then the code I mentioned....
anyway something like this... I really am an amateur at coding but I did play around quite a bit a few years back!
Joined: Aug 2002
Posts: 181
Likes: 0
From: Surrey
Hi BOAC,
try your code with a space between the comma and the second email address.
also put a ' symbol directly before the first email address and another ' symbol directly after the second email address.
finally try saving the email addresses to a variable and then dropping that into the mail function as follows.
$to = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';
mail($to, $subject, $message, $headers);
// note no ' sybmols before or after the $to variable
failing that how about just looping your code so that you send the email twice - once to each address?
MrS
try your code with a space between the comma and the second email address.
also put a ' symbol directly before the first email address and another ' symbol directly after the second email address.
finally try saving the email addresses to a variable and then dropping that into the mail function as follows.
$to = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';
mail($to, $subject, $message, $headers);
// note no ' sybmols before or after the $to variable
failing that how about just looping your code so that you send the email twice - once to each address?
MrS
Last edited by mrsurrey; 20th January 2008 at 02:16.

Joined: Feb 2007
Posts: 755
Likes: 26
From: Dublin, Ireland. (No, I just live here.)
Does it matter whether 'single' or "double" quotes are used? I think you should be using single quotes, as per that PHP website. Your first code snippet had no quotes, your second had double quotes...
All the '.' operators are doing, in that PHP code, is concatenating the strings - it shouldn't be a requirement. So it looks to me like you need the addresses surrounded by single quotes, with a comma and a space between them.
'mail('[email protected], [email protected]', 'TITLE', $message,'From: email addy');
All the '.' operators are doing, in that PHP code, is concatenating the strings - it shouldn't be a requirement. So it looks to me like you need the addresses surrounded by single quotes, with a comma and a space between them.

'mail('[email protected], [email protected]', 'TITLE', $message,'From: email addy');
Thread Starter
Per Ardua ad Astraeus
Joined: Mar 2000
Posts: 18,575
Likes: 4
From: UK
Thanks for those ideas - I THINK I've tried them all but will re-do today.
As I thought, had tried all those and it would NOT play ball with multiple addys.
Hats off to MrSurrey for kicking the brain cell into life. Simply put a duplicate
mail('[email protected]', 'TITLE', $message,'From: email addy');
in the script and BINGO. Thanks to all for ideas.
As I thought, had tried all those and it would NOT play ball with multiple addys.
Hats off to MrSurrey for kicking the brain cell into life. Simply put a duplicate
mail('[email protected]', 'TITLE', $message,'From: email addy');
in the script and BINGO. Thanks to all for ideas.
Joined: Jan 2008
Posts: 6
Likes: 0
From: Merseyside
to parameter needs spaces
http://uk.php.net/manual/en/function.mail.php
If that doesn't work then yes, your workaround will - call mail() twice. Fine except you don't really want to call it too much as automating that function can be construed as spam and you don't want that risk.




