PHP Mail () Function question?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sl82t
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:31 pm

PHP Mail () Function question?

Post by sl82t »

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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Firstly (and not related to the actual problem): you can change things like these:

Code: Select all

$msg="$message"; 

$subject = "$subject"; 

// and

$to = "$ned1" . ", "; 
$to .= "$ned2" . ", ";
to

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.',';
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:

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>'; 

?>
Mac
sl82t
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:31 pm

Post by sl82t »

Hmmm.....It is giving me an error when I do the implode???

It says:
Warning: Bad arguments to implode() in /var/www/html/sendmail.php on line 13

Here is what I have typed:
$to = implode(';', $to);

is there different syntax for that?

Thanks,
sl82t
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The syntax is fine - but it appears there's a problem with the arguments. I tested the code I posted on my own machine so it should run. Could we see your latest code?

Mac
sl82t
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:31 pm

Error fixed...but I don't get e-mail

Post by sl82t »

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
sl82t
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:31 pm

here is my HTML Code also...

Post by sl82t »

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
sl82t
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:31 pm

running code...

Post by sl82t »

The code runs....it just doesn't e-mail anyone....I am wondering if it is my HTML Form that is incorrect?

thanks,
sl82t
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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

Code: Select all

&lt;html&gt; 
&lt;head&gt; 
&lt;title&gt;E-mail Form&lt;/title&gt; 
&lt;/head&gt; 

&lt;body&gt; 

&lt;form method="POST" action="test.php"&gt; 
&lt;h1 align="center"&gt;E-mail Page&lt;/h1&gt; 
&lt;p&gt;First Name: &lt;input type="text" name="sender_fname" size="25" maxlength="25" /&gt;&lt;/p&gt; 
&lt;p&gt;Last Name: &lt;input type="text" name="sender_lname" size="25" maxlength="25" /&gt;&lt;/p&gt; 
&lt;p&gt;E-mail Address: &lt;input type="text" name="sender_email" size="30" /&gt;&lt;/p&gt; 

&lt;p&gt;Who would you like to e-mail today?&lt;br /&gt; 
&lt;input type="checkbox" name="email&#1111;]" value="sl82t@cc.usu.edu" /&gt;Ned Adams&lt;br /&gt; 
&lt;input type="checkbox" name="email&#1111;]" value="adams_ned@hotmail.com" /&gt;Ned Adams (Hotmail)&lt;br /&gt; 
&lt;input type="checkbox" name="email&#1111;]" value="sl82t@cc.usu.edu" /&gt;Joyce Adams&lt;/p&gt; 

&lt;p&gt;Subject: &lt;input type="text" name="subject" size="30" /&gt;&lt;/p&gt; 
&lt;p&gt;Message: &lt;textarea name="message" cols="30" rows="5"&gt;&lt;/textarea&gt;&lt;/p&gt; 
&lt;input type="hidden" name="action" value="send" /&gt;
&lt;p&gt;&lt;input type="submit" value="Send the Form" /&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;/body&gt; 
&lt;/html&gt;
and then this for the processing code:

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 

}

?>
Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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:

Code: Select all

$to[] = "$ned1"; 
$to[] = "$ned2";
should be

Code: Select all

$to[] = $ned1; 
$to[] = $ned2;
in other cases

Code: Select all

$subject = "$subject";
can be deleted as all it is is:

Code: Select all

$subject = $subject;
which is setting one variable equal to itself and thus does nothing useful.

Code: Select all

$msg = "$message";
can also be deleted and

Code: Select all

mail($to, $subject, $msg, $mailheaders);
changed to

Code: Select all

mail($to, $subject, $message, $mailheaders);
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
Post Reply