Process order form with php to send email - php

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..

Related

Tell a friend script not sending email php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to execute "Tell a friend" php script, but that is not sending any emails, not to admin, friends, sender. Other emails on same server working fine.
I don't understand why this is not working, as other emails pages like contact us, registration (which send confimation email) all working on same server, please help me..
Html Code:
<table>
<tr>
<td>
<span>Complete the details below to send this link to a friend:</span>
<?php
$refurl = $_SERVER['HTTP_REFERER'];?>
<span><? print $refurl;?></span>
<form name="tellafriend" action="send_group.php" method="post" onSubmit="return checkfields()">
<table>
<tr>
<td> Your name*:</td>
<td> <input name="name" size="30" maxlength="45"> </td>
</tr>
<tr>
<td>Your email*:</td>
<td><input name="email" size="30" maxlength="45"></td>
</tr>
<tr>
<td colspan="2"><p align="center">Enter your friend's email addresses:</p>
</td>
</tr>
<tr>
<td>Email 1*:</td>
<td><input name="fmail1" class="bordesolid1" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 2*:</td>
<td><input name="fmail2" size="30" maxlength="50"></td>
</tr>
<tr>
<td>Email 3*:</td>
<td><input name="fmail3" size="30" maxlength="50"></td>
</tr>
<tr>
<td colspan="2"><p align="center"><span>This message will contain your name & email address.</span>
<br>
<input onClick="validate();" type="button" value="click once to send">
<input type=hidden name=refurl value="<? print $refurl;?>">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP Code:
<?php
if(count($_POST)) {
foreach(array('fmail1','fmail2','fmail3','email','name') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) {
die("Peace People! Stop Spamming!");
}
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$to = "arvindsri123#yahoo.com";
$subject = "Recommendation form submission";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">'.$name.' has used your recommendation form using an email address of '.$email.' </p>';
$message.='<p style="margin-top:10px;">The people the recommendation has been submitted to are: </p>';
$message.='<p style="margin-top:10px;">'.$fmail1.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail2.' </p>';
$message.='<p style="margin-top:10px;">'.$fmail3.' </p>';
$message.='<p style="margin-top:10px;">The page recommended:</p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($to, $subject, $message, $headers);
// $thankyoupage = "thankyou.htm";
//echo $sentmail;
if($sentmail) {
$name = $_POST[name];
$email = $_POST[email];
$fmail1 = $_POST[fmail1];
$fmail2 = $_POST[fmail2];
$fmail3 = $_POST[fmail3];
$refurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$tsubject = "A web page recommendation from $_POST[name]";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message.='<p style="margin-top:10px;">Hi, '.$name.' whose email address is $_POST[email] thought you may be interested in this web page. '.$email.' </p>';
$message.='<p style="margin-top:10px;">'.$refurl.'</p>';
$message .= '</body></html>';
$sentmail = mail($fmail1,$fmail2,$fmail3, $tsubject $message, $headers);
echo '<h4>You have sent emails...</h4>';
//header("Location: $thankyoupage");
exit;
}
function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) {
return preg_match($reg,$ar);
}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}
function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}
?>
I am using Bluehost web hosting, and other emails properly working as I said Contact us, Registration confirmation emails.. etc.
Thanks!
If you are running on localhost then it won't send email.. to do so you need to host on server
Try to host on free hosting server.. it will work!

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

PHP Form processing not send proper email

I have the follwing form which is being populated via autosuggest from a database
<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>
<form id="itemsForm" action="services/processOrder.php" method="post">
<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>
and here is the php query for the autosuggest:
<?php
require_once('db_connection.php');
$return_arr = array();
$param = $_GET["term"];
$query = "SELECT field_id_5, exp_weblog_titles.title, field_id_57
FROM exp_weblog_data, exp_weblog_titles
WHERE exp_weblog_titles.entry_id = exp_weblog_data.entry_id AND field_id_5
LIKE '%". $param ."%'
LIMIT 10";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch_assoc()) {
$row_array['jItemCode'] = $row['field_id_5'];
$row_array['jItemDesc'] = $row['title'];
$row_array['jItemPrice'] = $row['field_id_57'];
//$row_array['jItemWholesale'] = $row['itemWholesale'];
//$row_array['jItemRetail'] = $row['itemRetail'];
// $row_array['jQtyOnHand'] = $row['qtyOnHand'];
array_push( $return_arr, $row_array );
}
$result->free_result();
$mysqli->close();
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
Finally here is the php for send the form as an email:
<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: " . $_REQUEST['repName'] . ">\r\n";
$to = 'dominic.mcafee#pmcnttyyre.com';
$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";
}
?>
All I get in the email is "We have recieved the following information:
Code:
Description:
Price:
"
Please can someone help me out. Where am I going wrong. I know Im close and the problem lies somewhere in referencing the array of data that is in the form.
Hard to say from code, but main reason is that $_REQUEST[$a] is empty. So try to echo whole $_REQUEST right after submitting your form for better debugging.
Also I dont see submit button and also end of the form tag - it could be bug too.

Form with multiple email fields to multiple recipients

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";

Categories