We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to add recipients on a PHP form?

Guilov
Guilov asked
on
Medium Priority
913 Views
Last Modified: 2013-12-13
Hi,

The code below has a  $to and  $cc fields. How can I add more recipients? I tried separating emails with a coma but that doesnt work.

Sorry. I am a new php user and need to have this done asap.

best.

RC


<?php

  $name = $_POST['firstname'] . ' ' . $_POST['lastname'];
  $address = $_POST['address'] . ' ' . $_POST['city'] . ' ' . $_POST['state']  . ' ' . $_POST['zip'];
 
  $email = $_POST['email'];
  $petition = $_POST['petition'];
  $sponsor = $_POST['sponsor'];
  $organization = $_POST['organization'];
 
  $to = 'rcortez@hivlawproject.org';
  $cc = 'rosa.e.cortez@gmail.com';
  $subject = 'Support Comprehensive Sex Education in New York City Schools';
  $msg = "$petition \n".
    "$name\n".
       "$address\n".
      "$email\n" .
      "$organization\n" .
      "$sponsor \n";
      
  mail($to, $subject, $msg, 'From:' . $email . "\r\nCc:" . $cc);
 echo("<script language='javascript'>window.location='http://hivlawproject.org/petition/thankyou.html';</script>");
 ?>
<?php
 
  $name = $_POST['firstname'] . ' ' . $_POST['lastname'];
  $address = $_POST['address'] . ' ' . $_POST['city'] . ' ' . $_POST['state']  . ' ' . $_POST['zip'];
  
  $email = $_POST['email'];
  $petition = $_POST['petition'];
  $sponsor = $_POST['sponsor'];
  $organization = $_POST['organization'];
 
  $to = 'rcortez@hivlawproject.org';
  $cc = 'rosa.e.cortez@gmail.com';
  $subject = 'Support Comprehensive Sex Education in New York City Schools';
  $msg = "$petition \n".
    "$name\n".
	 "$address\n". 
	"$email\n" .
	"$organization\n" .
	"$sponsor \n";
	
  mail($to, $subject, $msg, 'From:' . $email . "\r\nCc:" . $cc);
 echo("<script language='javascript'>window.location='http://hivlawproject.org/petition/thankyou.html';</script>");
 ?>

Open in new window

Comment
Watch Question

Commented:
Try putting all email addresses in an array and make a loop

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thank you. I did that. But now, the email is not being sent to the cc list. Please look at the code and let me know what I need to do next. I think I am missing the loop thing.

 $cc =array('rosa.e.cortez@gmail.com','info@hivlawproject.org','twelsh@hivlawproject.org');

mail($to, $subject, $msg, 'From:' . $email . "\r\nCc:" . $cc);
 $cc =array('rosa.e.cortez@gmail.com','info@hivlawproject.org','twelsh@hivlawproject.org');
 
mail($to, $subject, $msg, 'From:' . $email . "\r\nCc:" . $cc);

Open in new window

Commented:
Do you want to send one mail from $to with cc from $cc? Try this
$cc =array('rosa.e.cortez@gmail.com','info@hivlawproject.org','twelsh@hivlawproject.org');
$to =array('abc@gmail.com','info@abc.org','twelsh@abc.org');
 
// count = number of emails
$num = count($cc);
 
// and index [$i] grows up and picks the next email from one and the other array
for($i=0;$i<$num;$i++)	{
  mail($to[$i], $subject, $msg, 'From:' . $email . "\r\nCc:" . $cc[$i]);
}

Open in new window

Commented:
The above will work only if you have an equal number of TO emails and CC emails in the arrays.

Author

Commented:
What if I only have 2 in the TO and 3 in the CC?
Commented:
then you have to know to whom to send what.

Perhaps a way out would be to have only one array as follows
  $a = array(to,cc,to,cc,to,'0...)

where every other email would be cc. If you do not want to use cc, then assign it zero.
Hope this will help

Author

Commented:
Thank you. You have been of great help. !
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.