Link to home
Start Free TrialLog in
Avatar of Dusty
DustyFlag for United States of America

asked on

PHP form mail question

Trying to get this form sent to multiple recipients, I dont know php but shouldnt this work?  


function emailFormSubmission()
{
      $to  = 'me@home.com' . ', ';
      $to .= 'you@home.com';
      $subject = 'Message from contact form';

---------------------------------------------------------------

the below code works when sending to a single address:
function emailFormSubmission()
{
      $to = 'you@home.com';
      $subject = 'Message from contact form';

--------------------------------

but I need to send to 2 address.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First, Isuggest you to read this article: And by the way, I am New to PHP

About multiple emails, you can use PhpMailer or you can iterate through an array:

Trivial example:
function emailFormSubmission()
{
      $to = array('me@home.com', 'you@home.com');
      $subject = 'Message from contact form';
      $message = 'message';
      foreach ($to as $recipient)
      {
            mail($recipient, $subject, $message);
      }
}

Open in new window

Please don't link to PHPMailer on Worxware; It's not been on there for years. It's now on GitHub.

Secondly, avoid calling mail() yourself. You'll probably be doing it wrong - use a library.

If you're sending the same message to both people, if they know each other, just add both as 'to' recipients of the same message, or CC one of them. If they don't, either use BCC or send two separate messages. There are plenty of code examples in the PHPMailer repo.
And even if that code would work, the variables you are using will only be 'local', not 'global' and be invisible to any other routines.
Avatar of Dusty

ASKER

Thanks!