Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem sending multiple emails from a mail () form

Newbie
 
Join Date: Nov 2008
Posts: 7
#1: Nov 17 '08
Hi. I'm a relative newcomer to the world of php, and I keep bumping into a problem with a mail () form. I need to send an automatic email to two addresses, but I can't seem to get it to work. One email address works just fine, but I can't get the email sent to two different addresses no matter what I try. Below is my code. If someone could help me spot my errors, I'd appreciate it. Thanks!

Code from actual page:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //Check if the form has been submitted.
  3. if ( isset ($_POST['submit'])) {
  4.  
  5. $problem = FALSE; 
  6.  
  7. //Check for each value.
  8.  
  9. if (empty ($_POST['email2'])) {
  10. $problem = TRUE;
  11. print '<p>Please enter your email address!</p>';
  12. }
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19. //Display the form.
  20. print '<form action="handle_requests.php" method="post"><p>';
  21.  
  22. print 'Email Address: <input type="text" name="email2" size="20" ' . $_POST['email2'] . '" /> <br />';
  23.  
  24. print '<input type="submit" name="submit" value="Submit Email Address" /></p>
  25. </form>';
  26.  
  27. // Complete the HTML formatting stuff.
  28. print '</div>';
  29.  
  30. ?>
  31.  
  32. Code from handle_requests page:
  33.  
  34. <html>
  35. <head>
  36. <title>Request for E-mail Updates Successful!</title>
  37. </head>
  38.  
  39. <body>
  40.  
  41. <?php // Script to handle e-mail requests for updates.
  42.  
  43. //Address error handing.
  44. ini_set ('display_errors', 1);
  45. error_reporting (E_ALL & ~E_NOTICE);
  46.  
  47. // In case register_globals is disabled.
  48. $email2 = $_POST['email2'];
  49.  
  50.  
  51.  
  52. print '<p>Your request for e-mail updates has been successful! You will be receiving a e-mail confirmation shortly.</p>';
  53.  
  54. $body = "Thank you for requesting updates. Periodically, we will be sending you information about new material on our website, new publications, and upcoming events. You have registered with the following address: {$_POST['email2']}.";
  55. mail ($_POST['email2, address@email.com'], 'Email Confirmation', $body, 'From: address@email.com');
  56.  
  57. ?>
  58. </body>
  59. </html>

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#2: Nov 17 '08

re: Problem sending multiple emails from a mail () form


Hey there, wktarin, and welcome to the forums! Glad to have you here.

Please notice that I have formatted the code you posted in your previous post. To do this just highlight the code and then hit the '#' at the top of this editor. Doing this keeps you inside the laws of the Posting Guidelines. Give them a read.

Moderator.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#3: Nov 17 '08

re: Problem sending multiple emails from a mail () form


I don't quite get what you're trying to do here
Expand|Select|Wrap|Line Numbers
  1. mail ($_POST['email2, address@email.com'],
but try changing it to
Expand|Select|Wrap|Line Numbers
  1. mail ("{$_POST['email2']}, address@email.com",
Mail().
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 857
#4: Nov 18 '08

re: Problem sending multiple emails from a mail () form


If you are trying to send the same email to two different addresses you need either send it to yourself and then the 2 real targets in the BCC or process the email addresses in a loop sending the email twice, in each case to only one email address.

I normally build up an array of email addresses and then loop through the array, for each element in the array call the mail() function to send the email to one recipient only. this methodology protects peoples email addresses and respects their privacy.

Cheers
nathj

PS I have an emailObject() class that I use on most of my projects, if you want I can send you a copy of this code and some usage instructions. I think we can do that within Bytes.
Newbie
 
Join Date: Nov 2008
Posts: 7
#5: Nov 18 '08

re: Problem sending multiple emails from a mail () form


@Markus: Sorry about the code gaffe. Must have missed that when I scanned the guidelines. Thanks for your response. I knew my problem had to be something ridiculously simple... But it's fixed and working now!

@nathj: The form will be sent only to the company email address and the subscriber, so the blind carbon isn't a necessity. But thank you for the reminder.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#6: Nov 18 '08

re: Problem sending multiple emails from a mail () form


Quote:

Originally Posted by wktarin

@Markus: Sorry about the code gaffe. Must have missed that when I scanned the guidelines. Thanks for your response. I knew my problem had to be something ridiculously simple... But it's fixed and working now!

@nathj: The form will be sent only to the company email address and the subscriber, so the blind carbon isn't a necessity. But thank you for the reminder.

Glad you got it working.

Good day.
Reply