Solved

Send an email to mulitiple addresses

Posted on 2014-01-24
7
342 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
  • 4
  • 3
7 Comments
 
LVL 108

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 108

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 108

Accepted Solution

by:
Ray Paseur earned 500 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now