?
Solved

Send an email to mulitiple addresses

Posted on 2014-01-24
7
Medium Priority
?
348 Views
Last Modified: 2014-02-01
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
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39807602
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
 
LVL 7

Author Comment

by:rgranlund
ID: 39807622
@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
 
LVL 7

Author Comment

by:rgranlund
ID: 39807637
@Ray
Like so?

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

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39807661
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39807669
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
 
LVL 7

Author Comment

by:rgranlund
ID: 39807675
@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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39807697
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question