• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Send an email to mulitiple addresses

I have a form the will send an email to the recipient you put in the email field.  However, I want to have more than one email recipient field, I would like to have 5 or 6 recipient fields. I want to send the email to each of the emails that are entered, however, I'm not sure how to write the PHP and the instructions on the PHP page are not clear to me.  Any Help?

Here is my form so far:
function invite_friends_mailer() {
	echo '<form action="" method="POST">
		<div id="invite_friends_cont">
			<div class="mail-left">
				<label>FROM:</label>';
				bp_displayed_user_fullname();
		echo '</div>
			<div class="mail-right">
				<label>FROM EMAIL ADDRESS:</label>';
				bp_displayed_user_email();
		echo '</div>
			<div class="mail-left">
				<label class="label-150">Email Address:</label><input type="text" name="email_a"/>
			</div>
			
			<div class="mail-right">
				<label class="label-150">Email Address B:</label><input type="text" name="email_b"/>
			</div>
			
			<div class="mail-left">
				<label class="label-150">Subject:</label><input type="text" name="subject" value="I Am Raising Money"/>
			</div>
			
			<div class="mail-left">
				<label class="label-150">Message:</label><textarea name="comments"></textarea>
			</div>
			
			<div class="mail-left">
				<input type="submit" value="Send Invite"/>
			</div>
		</div>
	</form>
	
	';
}

Open in new window

0
rgranlund
Asked:
rgranlund
  • 4
  • 3
1 Solution
 
Ray PaseurCommented:
I'll skip the lecture about why this is a bad idea and you should use ConstantContact instead.

There are two ways to do this.  Either iterate over the POST inputs sending one mail message to each of the email addresses or put the email addresses into the "to" line separated by commas.  You might want to consider whether messages with several addresses in the "to" line will trigger a spam filter.
0
 
rgranlundAuthor Commented:
@ray, I know this is a bad idea, I'm just coding what someone asked me to code.  I'm not sure how to place the multiple email address in the "To:"
0
 
rgranlundAuthor Commented:
@Ray
Like so?

<?php
mail('To:'.$email.', ','.$email_b.','.$email_c.', 'Hello',  $body,  'From:rgranlund@EMAIL.com');
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ray PaseurCommented:
Yes, that would work if you have copied the $email and $email_xxx variables from the $_POST array and filtered them with filter_var().
0
 
Ray PaseurCommented:
Actually this would probably work better:
<?php
error_reporting(E_ALL);

// ASSUMES THE VARIABLES HAVE BEEN FILTERED!
$to 
= $email 
. ',' 
. $email_b
. ','
. $email_c
;
mail($to, 'Hello',  $body,  "From:rgranlund@EMAIL.com\r\n");

Open in new window

0
 
rgranlundAuthor Commented:
@Ray
is it ok if I filter with "preg_match" and "mysqli_real_escape_string "?

I know the following is wrong.  How do I write it correctly?
			if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,60}$/', $trimmed['email'])) {
				$e = mysqli_real_escape_string ($trimmed['email']);
			} else {
				$e = "";
			}

Open in new window

0
 
Ray PaseurCommented:
Not really.  Regular expressions are very hard to get right.  I've seen hundreds of regular expressions that purport to be able to validate an email address and almost all of them have something wrong.  See http://xkcd.com/1171/  That's why PHP introduced the filters.

The SQL escape functions are only used to prepare data for use in a query string.  They are not applicable to email.

Here is my function to check for valid emails.  If you're at a current PHP level it will use the filter, and will only fall back if you're on a back-level version of PHP.

<?php // RAY_email_validation.php
error_reporting(E_ALL);


// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
// SEE MAN PAGE: http://php.net/manual/en/intro.filter.php
function check_valid_email($email, $rout=TRUE)
{
    // LIST OF BLOCKED DOMAINS
    $bogus = array
    ( '@unknown.com'
    , '@example.com'
    , '@gooseball.org'
    )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                        // START REGEX DELIMITER
        . '^'                        // START STRING
        . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                        // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
        . '$'                        // ENDOF STRING
        . '/'                        // ENDOF REGEX DELIMITER
        . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email)) return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy)
    {
        if (stripos($email, $badguy)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    if ($rout)
    {
        $domain = explode('@', $email);

        // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
        if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") ) return TRUE;

        // EMAIL IS NOT ROUTABLE
        return FALSE;
    }
    return TRUE;
}



// DEMONSTRATE THE FUNCTION IN ACTION
$e = NULL;
if (!empty($_GET["e"]))
{
    $e = $_GET["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    }
    else
    {
        echo "<br/>BOGUS: $e \n";
    }
}


// END OF PROCESSING - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="$e" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

HTH, ~Ray
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now