PHP Mail () Function question?
Moderator: General Moderators
PHP Mail () Function question?
I am trying to send multiple e-mails using the mail() function. Here is my code. The first e-mail it actually e-mails...but the second does not...can anyone help me? Also, I am using a HTML form to get the variables.
<?php
$msg="$message";
$subject = "$subject";
$mailheaders = "From: Ned's Form Page \r\n";
$mailheaders = "Reply-To: $sender_email\r\n";
$to = "$ned1" . ", ";
$to .= "$ned2" . ", ";
mail($to, $subject, $msg, $mailheaders);
echo "<html><head><title>Email Sent!</title></head><body>";
echo "<h1 align=center>Thank you, $sender_fname</h1>";
echo "<p align=center>Your e-mail has been sent.</p>";
echo "<center>Return to <a href='email.html'>e-mail</a> more people</center>";
echo "</body></html>";
?>
Thanks
sl82t
<?php
$msg="$message";
$subject = "$subject";
$mailheaders = "From: Ned's Form Page \r\n";
$mailheaders = "Reply-To: $sender_email\r\n";
$to = "$ned1" . ", ";
$to .= "$ned2" . ", ";
mail($to, $subject, $msg, $mailheaders);
echo "<html><head><title>Email Sent!</title></head><body>";
echo "<h1 align=center>Thank you, $sender_fname</h1>";
echo "<p align=center>Your e-mail has been sent.</p>";
echo "<center>Return to <a href='email.html'>e-mail</a> more people</center>";
echo "</body></html>";
?>
Thanks
sl82t
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Firstly (and not related to the actual problem): you can change things like these:
to
because you don't need to put (and it's fairly pointless to put) double quotes around variables, you can just put the variable name.
But as for the actual problem, the code worked for me but maybe you could try semi-colons instead of commas to separate the different e-mail addresses:
Mac
Code: Select all
$msg="$message";
$subject = "$subject";
// and
$to = "$ned1" . ", ";
$to .= "$ned2" . ", ";Code: Select all
$msg = $message; // although do you really need this temp variable?
$subject = $subject; // you could delete this as all it does is set $subject equal to itself
// and
$to = $ned1.',';
$to. = $ned2.',';But as for the actual problem, the code worked for me but maybe you could try semi-colons instead of commas to separate the different e-mail addresses:
Code: Select all
<?php
$mailheaders = "From: Ned's Form Page \r\n";
// you need to add a period (.) to concenate the $mailheader variable
// otherwise you overwrite it with the second statement.
$mailheaders .= "Reply-To: $sender_email\r\n";
// could put all of the e-mails in an array which could make it easier
// to put them together
$to[] = $ned1;
$to[] = $ned2;
// join the addresses together with semicolons between them
$to = implode(';', $to);
mail($to, $subject, $message, $mailheaders);
echo '<html><head><title>Email Sent!</title></head><body>';
echo '<h1 align="center">Thank you, '.$sender_fname.'</h1>';
echo '<p align="center">Your e-mail has been sent.</p>';
echo '<p align="center">Return to <a href="email.html">e-mail</a> more people</p>';
echo '</body></html>';
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Error fixed...but I don't get e-mail
I got that error fixed...but now it won't e-mail at all?
Here is my code....can I see your working copy?
<?php
$msg= "$message";
$subject = "$subject";
$mailheaders = "From: Ned's Form Page \r\n";
$mailheaders .= "Reply-To: $sender_email\r\n";
$to[] = "$ned1";
$to[] = "$ned2";
$to = implode(';', $to);
mail($to, $subject, $msg, $mailheaders);
echo "<html><head><title>Email Sent!</title></head><body>";
echo "<h1 align=center>Thank you, $sender_fname</h1>";
echo "<p align=center>Your e-mail has been sent.</p>";
echo "<center>Return to <a href='email.html'>e-mail</a> more people</center>";
echo "</body></html>";
?>
Thanks,
sl82t
Here is my code....can I see your working copy?
<?php
$msg= "$message";
$subject = "$subject";
$mailheaders = "From: Ned's Form Page \r\n";
$mailheaders .= "Reply-To: $sender_email\r\n";
$to[] = "$ned1";
$to[] = "$ned2";
$to = implode(';', $to);
mail($to, $subject, $msg, $mailheaders);
echo "<html><head><title>Email Sent!</title></head><body>";
echo "<h1 align=center>Thank you, $sender_fname</h1>";
echo "<p align=center>Your e-mail has been sent.</p>";
echo "<center>Return to <a href='email.html'>e-mail</a> more people</center>";
echo "</body></html>";
?>
Thanks,
sl82t
here is my HTML Code also...
Here is my HTML code if that really matters?
<html>
<head>
<title>E-mail Form</title>
</head>
<body>
<form method="POST" action="sendmail.php">
<center><h1>E-mail Page</h1></center>
First Name:
<input type="text" name="sender_fname" size="25" maxlength="25">
<br><br>
Last Name:
<input type="text" name="sender_lname" size="25" maxlength="25">
<br><br>
E-mail Address:
<input type="text" name="sender_email" size="30">
<br><br>
Who would you like to e-mail today?
<br>
<input type="checkbox" name="ned1" value="sl82t@cc.usu.edu"> Ned Adams
<br>
<input type="checkbox" name="ned2" value="adams_ned@hotmail.com"> Ned Adams (Hotmail)
<br>
<input type="checkbox" name="joyce1" value="sl82t@cc.usu.edu"> Joyce Adams
<br><br>
Subject:
<input type="text" name="subject" size="30">
<br><br>
Message
<textarea name="message" cols=30 rows=5></textarea>
<br><br>
<input type="submit" value="Send the form">
</form>
</body>
</html>
thanks,
sl82t
<html>
<head>
<title>E-mail Form</title>
</head>
<body>
<form method="POST" action="sendmail.php">
<center><h1>E-mail Page</h1></center>
First Name:
<input type="text" name="sender_fname" size="25" maxlength="25">
<br><br>
Last Name:
<input type="text" name="sender_lname" size="25" maxlength="25">
<br><br>
E-mail Address:
<input type="text" name="sender_email" size="30">
<br><br>
Who would you like to e-mail today?
<br>
<input type="checkbox" name="ned1" value="sl82t@cc.usu.edu"> Ned Adams
<br>
<input type="checkbox" name="ned2" value="adams_ned@hotmail.com"> Ned Adams (Hotmail)
<br>
<input type="checkbox" name="joyce1" value="sl82t@cc.usu.edu"> Joyce Adams
<br><br>
Subject:
<input type="text" name="subject" size="30">
<br><br>
Message
<textarea name="message" cols=30 rows=5></textarea>
<br><br>
<input type="submit" value="Send the form">
</form>
</body>
</html>
thanks,
sl82t
running code...
The code runs....it just doesn't e-mail anyone....I am wondering if it is my HTML Form that is incorrect?
thanks,
sl82t
thanks,
sl82t
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
What you may need to do is have a read of:
viewtopic.php?t=511
You probably need to start using the $_POST array.
You could have something like this for your form
and then this for the processing code:
Mac
viewtopic.php?t=511
You probably need to start using the $_POST array.
You could have something like this for your form
Code: Select all
<html>
<head>
<title>E-mail Form</title>
</head>
<body>
<form method="POST" action="test.php">
<h1 align="center">E-mail Page</h1>
<p>First Name: <input type="text" name="sender_fname" size="25" maxlength="25" /></p>
<p>Last Name: <input type="text" name="sender_lname" size="25" maxlength="25" /></p>
<p>E-mail Address: <input type="text" name="sender_email" size="30" /></p>
<p>Who would you like to e-mail today?<br />
<input type="checkbox" name="emailї]" value="sl82t@cc.usu.edu" />Ned Adams<br />
<input type="checkbox" name="emailї]" value="adams_ned@hotmail.com" />Ned Adams (Hotmail)<br />
<input type="checkbox" name="emailї]" value="sl82t@cc.usu.edu" />Joyce Adams</p>
<p>Subject: <input type="text" name="subject" size="30" /></p>
<p>Message: <textarea name="message" cols="30" rows="5"></textarea></p>
<input type="hidden" name="action" value="send" />
<p><input type="submit" value="Send the Form" /></p>
</form>
</body>
</html>Code: Select all
<?php
// check if the form has been posted
if (!empty($_POST['action']) && $_POST['action'] == 'send') {
// go through the $_POST array and trim all non array values
// i.e. remove extra spaces and line breaks from around the
// user's input
foreach ($_POST as $key => $value) {
if (!is_array($value)) {
$value = trim($value);
}
// set the value to a variable of the name of the key
// the key would be something like 'sender_fname' and
// after this you would be able to use a variable called
// $sender_fname (the double dollar $$ is not a mistake)
$$key = $value;
}
$mailheaders = "From: Ned's Form Page \r\n";
$mailheaders .= "Reply-To: $sender_email\r\n";
// we've put all the e-mail's into an array via the form so
// now we can easily bring them together without duplicating
// any of them (as if there were two the same it wouldn't work
// on my system
$to = implode(';', array_unique($email));
mail($to, $subject, $message, $mailheaders);
?>
<html><head><title>Email Sent!</title></head><body>
<h1 align="center">Thank you, <?php echo $sender_fname; ?></h1>
<p align="center">Your e-mail has been sent.</p>
<p align="center">Return to <a href="email.html">e-mail</a> more people</p>
</body></html>
<?php
}
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
BTW, one thing that you keep doing in your code (which doesn't impact this problem but is something you should change) is putting variables within double quotes:
should be
in other cases
can be deleted as all it is is:
which is setting one variable equal to itself and thus does nothing useful.
can also be deleted and
changed to
because there's not much point setting up a variable called $msg that you only use once and is equal to $message when you can just use $message.
It'll make your code easier to read and debug in the long run.
Mac
Code: Select all
$to[] = "$ned1";
$to[] = "$ned2";Code: Select all
$to[] = $ned1;
$to[] = $ned2;Code: Select all
$subject = "$subject";Code: Select all
$subject = $subject;Code: Select all
$msg = "$message";Code: Select all
mail($to, $subject, $msg, $mailheaders);Code: Select all
mail($to, $subject, $message, $mailheaders);It'll make your code easier to read and debug in the long run.
Mac