Form with multiple email fields to multiple recipients - php

This is my html form. The user will input the email addresses he/she would like to send the html email to.
<form id="form1" name="form1" method="post" action="">
<table width="400">
<tr>
<td>Please enter your email address:</td>
<td<input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td>Please enter the email addresses you would like to notify below:</td>
<td>
</td>
</tr>
<tr>
<td>Email:</td>
<td>
<input type="text" name="email1" id="email1" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email2" id="email2" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email3" id="email3" />
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email4" id="email4" />
</td>
</tr>
</table>
</form>
This is somewhat the php code.
<?php
$ToEmail = '["email1"]["email2"]["email3"]["email4"]';
$EmailSubject = 'Check this out guys!';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: "noreply#domain.com"\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail(......) or die ("Failure");
?>
<script type="text/javascript">
alert("Success! You have sent the notification to the emails you have entered.");
<!--
window.location = "form.html"
//-->
</script>
How do I:
1. Modify the PHP code so that it will send to the emails inputed by the user?
2. The body message of the notification is a html email. How do I go about adding it to the PHP code?
Your help is highly appreciated. Thanks in advance!

Looks like you need to $_POST the email1, email2 etc. values to a variable then use that as your value for $to in the mail() function - just make sure you add a comma after each:
$to = $_POST['email1'] . ', ';
$to .= $_POST['email2'] . ', ';
$to .= $_POST['email3'];
etc. Leave off the comma for the last email and you should be ready to go.
Regarding the content of your email, you should be able to send html no problem - just store it in a variable for ease of use later, e.g:
$message = '
<html>
<head>
<title>This is the HTML Email</title>
</head>
<body>
<div id="container">
<p>Welcome to the html!</p>
<img src="../img/some_image.jpg" alt="some image"/>
</div>
</body>
</html>
';
then make sure you add the relevant HTML headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Along with any other headers e.g.:
$headers .= 'From: HTML Email <you#example.com>' . "\r\n";
Then call mail() with your defined variables:
mail($to, $subject, $message, $headers);
Hope that helps.
p.s. its all available on the mail function definition: mail()

Look at the php manual for mail
Example:
$toemails = "user#example.com, anotheruser#example.com";

Related

PHP Mail Headers Aren't Appearing

I'm trying to build a simple contact form for a website, but the $headers section of the email isn't being sent properly. The email itself is being sent, but everything under the $headers selector is missing from the email. I've included both the php code and the form below, and I'm also aware that I haven't sanitized the code yet. I want to make sure the form works properly before I add anything else.
<?php
$to = "example#gmail.com";
$subject = "Reply From Your Website: ".$_POST["subject"];
$message = $_POST["message"];
$headers = "From: ".$_POST["name"]."" . "\r\n" . "Reply To: ".$_POST["email"]."";
mail($to,$subject,$message,$headers);
?>
<table class="contact-form">
<form method="post">
<tr>
<td class="label">Name:</td>
<td class="input"><input type="text" maxlength="40" name="name" required></td>
</tr>
<tr>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" name="email" required></td>
</tr>
<tr>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" name="subject" required></td>
</tr>
<tr>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" maxlength="1000" name="message" required></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</form>
</table>
I'm not quite sure if this would help, but let's try it anyway.
<?php
$to = "kouen922#gmail.com";
$subject = "Reply From Your Website: ".$_POST["subject"];
$message = $_POST["message"];
$headers = "From: ".$_POST["name"]."" . "\r\n";
$headers .= "Reply To: ".$_POST["email"]."";
mail($to,$subject,$message,$headers);
?>
There could be possibly two reasons:
In $_POST['name'] you don't have e-mail address (the variable name says it's rather sender name, not e-mail)
The mail server configuration doesn't allow to send e-mails from domains different than server's domain
Try this:
$subject = "Reply From Your Website: ".$_POST["subject"];
$message = $_POST["message"];
$headers = "From: ".$_POST["name"]." <email#serverdomain.com>" . "\r\n" . "Reply To: ".$_POST["email"]."";
mail($to,$subject,$message,$headers);
but change email#serverdomain.com to some e-mail in the same domain as your server is.

PHP Form - not receiving email

I'm not understanding why Im not receiving email from the form after filling out the simple form. After clicking the submit button it redirected to the thank you page with no problem, but no email.
HTML
<form class="action" name="form1" method="POST" action="_sendmail2.php" onSubmit="return CheckAll(this);">
<label class="nick-2">Full Name:</label><br>
<input type="text" class="name" name="full_name">
<label class="nick">Email Address:</label><br>
<input type="text" class="email" name="email"><br>
<div class="radio-toolbar">
<input type="radio" id="radio1" name="agent_type" value="Buyer" checked>
<label for="radio1">Buyer</label>
<input type="radio" id="radio2" name="agent_type" value="Seller">
<label for="radio2">Seller</label>
<input type="radio" id="radio3" name="agent_type" value="Investor">
<label for="radio3">Investor</label>
</div><br>
<input type="submit" class="btn" value="SUBMIT" name="Submit">
</form>
PHP (
<?php
$to = "cluneborg#hotmail.com";
$subject = "New Agent Inquries";
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$agent_type = $_POST['agent_type'];
if($_SERVER['REQUEST_METHOD']=="POST") {
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$agent_type=str_replace ( array("\n"), array(" <br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$subject</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF> $agent_type</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'To: Eric <eluneborg#gmail.com>' . "\r\n";
$headers .= 'From: Texas Real Estate Agent Website' . "\r\n";
if(mail($to,$subject,$contentmsg,$headers)){
header("Location: http://www.magnixsolutions.com/clients/tas/thanks.html");
}
else
{
echo "Mail was not sent!";
}
}
?>
Sometimes it sends email to my hotmail and most of time it get this (checked on cpanel)
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
cluneborg#hotmail.com
Domain magnixsolutions.com has exceeded the max defers and failures per hour (5/5 (55%)) allowed. Message discarded.
(Tested) - There were a few issues with your code.
The most important thing, is the # symbol in #mail - This will not execute, it needs to be removed.
Now, this line: (in PHP)
$_REQUEST['type_agent']
should be:
$_REQUEST['agent_type']
as per: (in HTML form)
<input type="radio" id="radio3" name="agent_type" value="Investor">
Then your headers were incorrect, where I added a few \r\n
One of your headers (in PHP)
$headers .= "From: ".$from."";
has been changed to:
$headers .= "From: $full_name <$email>\r\n";
Sidenote: It could be replaced with
$headers .= "From: $fromemail <$email>\r\n";
if you want the name to appear as "New Agent" in mail, instead of the person's name sending the Email.
Using this $fromemail="New Agent"; in conjunction with $from=$fromemail; and $headers .= "From: ".$from."";
would have resulted in mail going to SPAM, being it's not an actual Email address.
Plus, upon testing your original code, it did not come in as proper HTML, but the codes themselves showed up in the Email; that has been corrected.
If you want the Email and the name, you need to use two different variables.
I.e.:
$headers .= 'From: YourName <YourName#domain.com>' . "\r\n";
and in your case:
$headers .= "From: $full_name <$email>\r\n";
Rewrite: (PHP)
<?php ob_start();
// commented out - is not needed for the time being
// $fromemail="New Agent"; // change here if you want
$toemail="email#example.com"; // change here if you want
$sub="Agent Inquiries"; // change here if you want
$success_page_name="thanks.html";
////// do not change in following
if($_SERVER['REQUEST_METHOD']=="POST")
{
$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));
$type_agent=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['agent_type']));
$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$full_name</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$email</td>
</tr>
<tr>
<td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
<td width=565 align=left valign=top bgcolor=#FFFFFF>$type_agent</td>
</tr>
</table>
");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: $full_name <$email>\r\n";
#mail($toemail,$sub,$contentmsg,$headers);
header("Location:$success_page_name");
}
?>
Footnotes:
Including the # symbol in #mail suppresses errors and does not execute the function, so you will want to remove it..
In my testing, I removed onSubmit="return CheckAll(this); since your full code didn't include that function. Should it fail, then you may need to remove it also.
Remove the # sign from the #mail command and it might give you a helpful error. The # sign there is suppressing errors:
http://www.php.net/manual/en/language.operators.errorcontrol.php
You need to add EOL character \n in headers to separate. Don't know whether this is the solution, but it is at least one problem that needs attention.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$from=$fromemail;
$headers .= "From: ".$from."\n";
In addition to the error suppression answers/comments, you can also make sure that mail() returns true indicating your server has accepted it and will attempt delivery.
$success = mail($toemail,$sub,$contentmsg,$headers);
var_dump( $success ); // should be true

Process order form with php to send email

I have the following code:
<table class="table table-striped" id="itemsTable">
<thead>
<tr>
<th></th>
<th>Item Code</th>
<th>Description</th>
<th>Qty</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class="item-row">
<td></td>
<td><input type="text" name="itemCode[]" value="" class="input-medium" id="itemCode"
tabindex="1"/>
</td>
<td><input type="text" name="itemDesc[]" value="" class="input-large" id="itemDesc"
readonly="readonly"/></td>
<td><input type="text" name="itemQty[]" value="" class="input-mini" id="itemQty" tabindex="2"/>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span>
<input
name="itemPrice[]"
class=" input-small"
id="itemPrice"
type="text"></div>
</td>
<td>
<div class="input-prepend input-append"><span class="add-on">€</span><input
name="itemLineTotal[]" class=" input-small" id="itemLineTotal" type="text"
readonly="readonly"></div>
</td>
</tr>
</tbody>
</table>
What is the best way to process the inputs via php to send the order via email nicley formated into a table? This is an order form and I need to to simply be sent to an email once complete
Here is my processing code:
<?php
$to = $_REQUEST['xxx'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"itemCode"} = "Code";
$fields{"itemDesc"} = "Description";
$fields{"itemPrice"} = "Price";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: noreply#example.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
$send = mail($to, $subject, $body);
if($send){
header( "Location:index.php" );
} else {
print "We encountered an error sending your mail, please try again";
}
?>
This code is not working please help
To process a form this way you need to have a form element somewhere in your markup to process.
<form method="POST" action="yourSecondScript.php">
your first markup here
<input type="submit">
<form>
Then to make the email nice with tables you need to set the email headers to html.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . $_REQUEST['Name'] . ">\r\n";
mail($to, $subject, $body, $headers);
Where is
$to = $_REQUEST['xxx'] ;
coming from? My guess is that you can set it to fixed as you probably don't need a dynamic e-mail to address, so something like:
$to = 'myname#mydomain.com';
But as Michael mentioned in a reaction to your question, we cannot be sure until you tell us which part is not working / what errors you receive and so on..

Simple php contact form issue, not sending

Building a website for a client but the contact form is refusing to work, I set the client up with an email account 'contact#eunicedcolvin.co.uk' but it doesn't appear to finish sending it off to load the success message, here is the code below. Im fairly new to PHP but can't figure out what the issue is.
<div id="contact-form" style="background: none;">
<div id="contact-header"></div>
<div id="contact-middle">
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'contact#eunicedcolvin.co.uk';
$EmailSubject = 'Website Contact';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Comment: ".n12br($_POST["comment"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<div id="contact-success">
<span style="font-size:16px; font-weight:bold; ">Thanks! Your message was sent</span><br>
I will get back to you as soon as I can
</div>
</div>
<?php
} else {
?>
<form method="POST">
<table width="360" border="0" cellspacing="8" cellpadding="0" style="margin-left: 11px;">
<tbody><tr>
<td class="contact-label">Your name:<br></td>
<td class="contact-text-border"><input name="name" type="text" id="name" size="32" class="contact-name"></td>
</tr>
<tr>
<td class="contact-label">Your Email address:</td>
<td class="contact-text-border"><input name="email" type="text" id="email" size="32" class="contact-name"></td>
</tr>
<tr>
<td class="contact-label">Comment:</td>
<td class="contact-text-border" style="padding: 2px 2px;"><textarea name="comment" cols="45" rows="6" id="comment" class="contact-comment"></textarea></td>
</tr>
<tr>
<td height="33" class="contact-label"> </td>
<td align="left" valign="top">
<input type="submit" name="Submit" class="submit" value="."></td>
</tr>
</tbody></table>
</form>
<?php
};
?>
</div>
<div id="contact-bottom"></div>
</div>
</div>
if you'd like to see a sample of it live, its here: http://www.eunicedcolvin.co.uk/contact.html
link to website
The thing is, originally it was set up to post to my personal email to test, then when i swapped it over it refused to work! been racking my brains over this for a while now :S
Your email sending server is most likely not setup correctly.
If it is in fact sending, then its probably get spam filtered.
Also you should not set the from address to the submitted address in the form, as your server will not be authorized to send from that domain.
To try your server is configured mail or not :
if(#mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
use this code, upload it to simple x.php file with just this code (mail etc is inserted),
you know that your server is just ok or not :)...
or might be it's put to spam folder of the receiver
Use SMTP.
Don't spend time trying to make mail work.... In my opinion it's just a waste of time, and most emails sent with it, end up in SPAM folder.

PHP email wont send variables

Basically the following code is an enquiry form that when the website user fills out, it sends the email to my email address. But something isn't working...
Here's the php:
<?php
$field_name = $_POST['efName'];
$field_email = $_POST['efEmail'];
$field_phone = $_POST['efPhone'];
$field_date = $_POST['efTravelDate'];
$field_duration = $_POST['efLengthOfStay'];
$field_otherInfo = $_POST['efOtherInfo'];
$mail_to = 'James_Burnside94#hotmail.com';
$subject = 'Enquiry Applicant-'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Date: '.$field_date."\n";
$body_message .= 'Duration: '.$field_duration."\n";
$body_message .= 'otherInfo: '.$field_otherInfo."\n";
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Enquiry Sent.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Enquiry form failed to send. \n Please try again, or send an email to enquiry#cannes-rental-apartment.com');
window.location = 'index.html';
</script>
<?php
}
?>
and here's the Html body:
<body>
<fieldset style="display:block; width:500px;">
<form action="enquiry.php" method="post">
<table>
<tr> <td> <span class="enquiryFormHeader"> Enquiry Form </span> <p> </p></td> </tr>
<tr> <td> Name: </td> <td> <input type="text" name="efName" id="efName"> </td> </tr>
<tr> <td> Email: </td> <td> <input type="text" name="efEmail" id="efEmail"> </td> </tr>
<tr> <td> Phone or Moblie: </td> <td> <input type="text" name="efPhone" id="efPhone"> </td> </tr>
<tr> <td> Intended travel date: </td> <td> <input type="text" name="efTravelDate" id="efTravelDate"> </td> </tr>
<tr> <td> Intended length of stay: </td> <td> <input type="text" name="efLengthOfStay" id="efLengthOfStay"> </td> </tr>
<tr> <td valign="top"> Any other information: </td> <td> <textarea name="efOtherInfo" id="efOtherInfo"> </textarea> <p> </p> </td> </tr>
<tr> <td><input type="submit" value="Send Form" class="submitButton"> </td> </tr>
</table>
</form>
</fieldset>
</body>
If i do
$mail_status = mail($mail_to, 'subject', 'body_message', 'headers');
it sends a basic email perfectly.
On all occasions the Enquiry Sent alert appears.
Anyone know why it wont work??
Run a var_dump(get_defined_vars()); and ensure that they are set. Also check /var/mail/user where user is the PHP or Apache user, and see what errors are thrown at more of an OS level.
1- Exactly, what do you get? An empty mail or no mail at all?
2- Try to debug your $_POST, like user1593858 said. Right after the php opening tag add this:
echo "<pre>";
print_r($_POST);
echo "</pre>";
and see if your fields are correct.
3- you say that if you do:
$mail_status = mail($mail_to, 'subject', 'body_message', 'headers');
it works; well try to substitute 'subject' with $subject or 'body_message' with $body_message until you isolate the problem.

Categories