Problem sending multiple emails from a mail () form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wktarin
    New Member
    • Nov 2008
    • 7

    Problem sending multiple emails from a mail () form

    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:

    Code:
    <?php
    //Check if the form has been submitted.
    if ( isset ($_POST['submit'])) {
    
    $problem = FALSE; 
    
    //Check for each value.
    
    if (empty ($_POST['email2'])) {
    $problem = TRUE;
    print '<p>Please enter your email address!</p>';
    }
    }
    
    
    
    
    
    //Display the form.
    print '<form action="handle_requests.php" method="post"><p>';
    
    print 'Email Address: <input type="text" name="email2" size="20" ' . $_POST['email2'] . '" /> <br />';
    
    print '<input type="submit" name="submit" value="Submit Email Address" /></p>
    </form>';
    
    // Complete the HTML formatting stuff.
    print '</div>';
    
    ?>
    
    [B]Code from handle_requests page[/B]:
    
    <html>
    <head>
    <title>Request for E-mail Updates Successful!</title>
    </head>
    
    <body>
    
    <?php // Script to handle e-mail requests for updates.
    
    //Address error handing.
    ini_set ('display_errors', 1);
    error_reporting (E_ALL & ~E_NOTICE);
    
    // In case register_globals is disabled.
    $email2 = $_POST['email2'];
    
    
    
    print '<p>Your request for e-mail updates has been successful! You will be receiving a e-mail confirmation shortly.</p>';
    
    $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']}.";
    mail ($_POST['[B]email2, address@email.com[/B]'], 'Email Confirmation', $body, 'From: address@email.com');
    
    ?>
    </body>
    </html>
    Last edited by Markus; Nov 17 '08, 09:45 PM. Reason: added # tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      I don't quite get what you're trying to do here
      Code:
      mail ($_POST['email2, address@email.com'],
      but try changing it to
      Code:
      mail ("{$_POST['email2']}, address@email.com",
      Mail().

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        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.

        Comment

        • wktarin
          New Member
          • Nov 2008
          • 7

          #5
          @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.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            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.

            Comment

            Working...