Jump to content

PHP Email Form that sends to more then one person


kvnmck18

Recommended Posts

Here's the code I've been using for my email form:

<?php$mymail = 'eeeeeeeeeeeeeeeeeeeeemail@kvn18.com';$cc = 'Subject for the Email';$BoDy = ' ';$BoDy = ' ';$FrOm = 'FROM:' .$_POST['name'];$FrOm .= '<';$FrOm .= $_POST['email'];$FrOm .= '>';$BoDy .= 'Name: ';$BoDy .= $_POST['name'];$BoDy .= "\n";$BoDy .= 'Email: ';$BoDy .= $_POST['email'];$BoDy .= "\n";$BoDy .= 'City: ';$BoDy .= $_POST['city'];$BoDy .= "\n";$BoDy .= 'State: ';$BoDy .= $_POST['state'];$BoDy .= "\n";$BoDy .= 'Zip: ';$BoDy .= $_POST['zip'];$BoDy .= "\n";$send = mail("$mymail", "$cc", "$BoDy", "$FrOm");if($send){echo '<html><head>';echo '<meta http-equiv="refresh" content="0;URL=http://www.myfunkysite.com/">';echo '</head><body>Random text...';echo '</body></html>';}?>

It works great, but I've been to do a couple of different things with this form:1) Send it to more than one email address a) Send one to the default email :) Send it also to a secondary email c) Send it to the email address that was entered from the form2) Sending all the data to added to an XML doc. a)Combining the so far two PHP codes :) PHPFORM to XML FORM:

<?$images = Array();function start_element($parser, $name, $attrs){	global $hmmm;	if($name == "whatever"){		array_push($hmmm, $attrs);	}}function end_element ($parser, $name){}$imagesXML_string = file_get_contents("xml.xml");$parser = xml_parser_create();xml_set_element_handler($parser, "start_element", "end_element");xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);xml_parse($parser, $imagesXML_string) or die("Error parsing XML document.");print "<br />";if($_POST['action'] == "ins"){	array_push($hmmm, Array(				"name" => $_POST['name'],				"email" => $_POST['email'],				"city" => $_POST['city'],				"state" => $_POST['state'],				"zip" => $_POST['zip']));				$hmmm_final = $hmmm;}else if($_POST['action'] == "del"){	$hmmm_final = Array();	foreach($hmmm as $whatever){		if($whatever['name'] != $_POST['namr']){			array_push($hmmm_final, $whatever);		}	}}$write_string = "<hmmm>";foreach($hmmm_final as $whatever){	$write_string .= "<whatever name=\"$whatever[name]\" email=\"$whatever[email]\" city=\"$whatever[city]\" state=\"$whatever[state]\" zip=\"$whatever[zip]\" />";}$write_string .= "</hmmm>";$fp = fopen("xml.xml", "w+");fwrite($fp, $write_string) or die("Error writing to file");fclose($fp);print "New Content Added Successfully";print "<br />";print "<a href=\"index.php\" title=\"return\">Return</a>";?></p>

I hope this is all possible.

Link to comment
Share on other sites

<?php$mymail = 'me@myemaildomain.com';$cc .= $_POST['subject'];$BoDy = ' ';$BoDy = ' ';$FrOm = 'FROM:' .$_POST['1'];$FrOm .= '<';$FrOm .= $_POST['3'];$FrOm .= '>';$BoDy .= 'Name: ';$BoDy .= $_POST['1'];$BoDy .= "\n";$BoDy .= 'Email: ';$BoDy .= $_POST['2'];$BoDy .= "\n";$BoDy .= 'Send to Alternative Person, Name: ';$BoDy .= $_POST['3'];$BoDy .= "\n";$BoDy .= 'Alternative Email: ';$BoDy .= $_POST['4'];$BoDy .= "\n";$BoDy .= 'Subject: ';$BoDy .= $_POST['subject'];$BoDy .= "\n";$BoDy .= 'Comment: ';$BoDy .= $_POST['comment'];$BoDy .= "\n";$send = mail("$mymail", "$cc", "$BoDy", "$FrOm");if($send){echo '<html><head>';echo '<meta http-equiv="refresh" content="0;URL=http://www.myfunkysite.com/">';echo '</head><body>Random text...';echo '</body></html>';}?>

This won't work:

<?php$mymail = 'email@yahoo.com';$mymail .= $_POST['3'];$cc .= $_POST['subject'];$BoDy = ' ';$BoDy = ' ';$FrOm = 'FROM:' .$_POST['1'];$FrOm .= '<';$FrOm .= $_POST['2'];$FrOm .= '>';$BoDy .= 'Name: ';$BoDy .= $_POST['1'];$BoDy .= "\n";$BoDy .= 'Email: ';$BoDy .= $_POST['2'];$BoDy .= "\n";$BoDy .= 'Send to Alternative Person, Name: ';$BoDy .= $_POST['3'];$BoDy .= "\n";$BoDy .= 'Alternative Email: ';$BoDy .= $_POST['4'];$BoDy .= "\n";$BoDy .= 'Subject: ';$BoDy .= $_POST['subject'];$BoDy .= "\n";$BoDy .= 'Comment: ';$BoDy .= $_POST['comment'];$BoDy .= "\n";$send = mail("$mymail", "$cc", "$BoDy", "$FrOm");if($send){echo '<html><head>';echo '<meta http-equiv="refresh" content="0;URL=http://www.microcenter.com/">';echo '</head><body>Random text...';echo '</body></html>';}?>

Link to comment
Share on other sites

If you want to implement the form and the handler in one script, let the handler check wether the submit button on the form has been clicked (else display the handler).Very roughly:if (submit has been clicked) { execute handler;} else { display form;}at least that's how I do this.If you already use mysql, you may want to create a database for all recepients, and let the handler check the number of database entries and 'loop' the handler that many times. I don't have the time to explain it in detail, but this is how I'd do it.Don't forget to add a notifier at the end of the handler, with a link to the homepage.

Link to comment
Share on other sites

And, again, if you want to send to more then one person, use the CC header. You have a variable called $cc, but you are using it as the subject. The CC header (which, in your code, will be in the $FrOm variable) will let you carbon copy multiple people in the email. There are examples on php.net of how to do this, in the reference for the mail function.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...