Solved

Send an email to mulitiple addresses

Posted on 2014-01-24
7
347 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 110

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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 110

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 110

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 110

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
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…
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 …

707 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