?
Solved

How to add recipients on a PHP form?

Posted on 2009-02-11
7
Medium Priority
?
894 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

0
Comment
Question by:Guilov
  • 4
  • 3
7 Comments
 
LVL 6

Assisted Solution

by:Fero45
Fero45 earned 1000 total points
ID: 23612799
Try putting all email addresses in an array and make a loop
0
 

Author Comment

by:Guilov
ID: 23613044
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

0
 
LVL 6

Accepted Solution

by:
Fero45 earned 1000 total points
ID: 23613178
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Assisted Solution

by:Fero45
Fero45 earned 1000 total points
ID: 23613377
The above will work only if you have an equal number of TO emails and CC emails in the arrays.
0
 

Author Comment

by:Guilov
ID: 23613435
What if I only have 2 in the TO and 3 in the CC?
0
 
LVL 6

Assisted Solution

by:Fero45
Fero45 earned 1000 total points
ID: 23613493
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
0
 

Author Closing Comment

by:Guilov
ID: 31545596
Thank you. You have been of great help. !
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 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