Solved

Send an email to mulitiple addresses

Posted on 2014-01-24
7
339 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
Comment Utility
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
Comment Utility
@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
Comment Utility
@Ray
Like so?

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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

15 Experts available now in Live!

Get 1:1 Help Now