It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a script that I wrote that pretty much emails form data and sends it to a txt file. My question is how do I exclude some of the message from being sent to the text file? I don't want to send to the text file this line :
$message .= "A quote will be forwarded to
Joe Greeninger before order is placed.\n";
please help! How?
<?php
//$silver_name_badges = !empty($_POST['silver_name_badges']) ? ($_POST['silver_name_badges'])) : false;
$SilverNameBadges = $_POST['Silver_Name_Badges'];
$CoffeeMug = $_POST['Coffee_Mug'];
$Plastic_Literature_Bag = $_POST['Plastic_Literature_Bag'];
$Amada_Bag = $_POST['Amada_bag'];
$Candy = $_POST['Candy'];
$Moist_Towlette = $_POST['Moist_Towlette'];
$Notepad_and_Pen = $_POST['Notepad_and_Pen'];
$Tuck_Box = $_POST['Tuck_Box'];
$Amada_Tie = $_POST['Amada_Tie'];
$Cap = $_POST['Cap'];
$name = $_POST['Attention_To'];
$Red_Lanyard = $_POST['Red_Lanyard'];
$White_Lanyard = $_POST['White_Lanyard'];
$Green_Lanyard = $_POST['Green_Lanyard'];
$Black_Lanyard = $_POST['Black_Lanyard'];
$Glass_LC2415_NT = $_POST['Glass_LC2415_NT'];
$Glass_FOM2NTRI = $_POST['Glass_FOM2NTRI'];
$Glass_ASTRO_165WNT = $_POST['Glass_ASTRO_165WNT'];
$Glass_FOL_AJ3015 = $_POST['Glass_FOL_AJ3015'];
$Glass_ACIES_NT = $_POST['Glass_ACIES_NT'];
$Notes = $_POST['Notes'];
$Ship_Preference = $_POST['Ship_Preference'];
// Please specify your Mail Server - Example: mail.yourdomain.com.
ini_set("SMTP","mail.amada-america.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'jgreeninger#amada.com');
// Set parameters of the email
$to = "ravila#amada.com";
$subject = "Amada Promo Items Ordered";
$from = " jgreeninger#amada.com";
$headers = "CC: ravila#amada.com";
$message = "";
$message .= date("m-d-Y\n");
$message .= "Order has been placed.
Items:\n";
foreach ($_POST as $fieldName => $fieldValue){
if (!empty($fieldValue))
$message .= " $fieldName: $fieldValue\n";
}
$message .= "A quote will be forwarded to
Joe Greeninger before order is placed.\n";
$message = str_replace("_"," ",$message);
// Mail function that sends the email.
mail($to,$subject,$message,$footer,$headers);
$fp = fopen("latc.txt", "a");
fwrite($fp, $message."\r\n");
fclose($fp);
header('Location: index.html');
?>
Change the order of things so you write the file before adding that line to the message:
foreach ($_POST as $fieldName => $fieldValue){
if (!empty($fieldValue))
$message .= " $fieldName: $fieldValue\n";
}
$message = str_replace("_"," ",$message);
$fp = fopen("latc.txt", "a");
fwrite($fp, $message."\r\n");
fclose($fp);
$message .= "A quote will be forwarded to
Joe Greeninger before order is placed.\n";
// Mail function that sends the email.
mail($to,$subject,$message,$footer,$headers);
Try this:
<?php
//$silver_name_badges = !empty($_POST['silver_name_badges']) ? ($_POST['silver_name_badges'])) : false;
$SilverNameBadges = $_POST['Silver_Name_Badges'];
$CoffeeMug = $_POST['Coffee_Mug'];
$Plastic_Literature_Bag = $_POST['Plastic_Literature_Bag'];
$Amada_Bag = $_POST['Amada_bag'];
$Candy = $_POST['Candy'];
$Moist_Towlette = $_POST['Moist_Towlette'];
$Notepad_and_Pen = $_POST['Notepad_and_Pen'];
$Tuck_Box = $_POST['Tuck_Box'];
$Amada_Tie = $_POST['Amada_Tie'];
$Cap = $_POST['Cap'];
$name = $_POST['Attention_To'];
$Red_Lanyard = $_POST['Red_Lanyard'];
$White_Lanyard = $_POST['White_Lanyard'];
$Green_Lanyard = $_POST['Green_Lanyard'];
$Black_Lanyard = $_POST['Black_Lanyard'];
$Glass_LC2415_NT = $_POST['Glass_LC2415_NT'];
$Glass_FOM2NTRI = $_POST['Glass_FOM2NTRI'];
$Glass_ASTRO_165WNT = $_POST['Glass_ASTRO_165WNT'];
$Glass_FOL_AJ3015 = $_POST['Glass_FOL_AJ3015'];
$Glass_ACIES_NT = $_POST['Glass_ACIES_NT'];
$Notes = $_POST['Notes'];
$Ship_Preference = $_POST['Ship_Preference'];
// Please specify your Mail Server - Example: mail.yourdomain.com.
ini_set("SMTP","mail.amada-america.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'jgreeninger#amada.com');
// Set parameters of the email
$to = "ravila#amada.com";
$subject = "Amada Promo Items Ordered";
$from = " jgreeninger#amada.com";
$headers = "CC: ravila#amada.com";
$message = "";
$message .= date("m-d-Y\n");
$message .= "Order has been placed.
Items:\n";
foreach ($_POST as $fieldName => $fieldValue){
if (!empty($fieldValue))
$message .= " $fieldName: $fieldValue\n";
}
$message = str_replace("_"," ",$message);
// Mail function that sends the email.
$mailMsg = $message . " A quote will be forwarded to Joe Greeninger before order is placed.\n";
mail($to,$subject,$mailMsg,$footer,$headers);
$fp = fopen("latc.txt", "a");
fwrite($fp, $message."\r\n");
fclose($fp);
header('Location: index.html');
?>
Related
I am using the following code to email the results of a form submission. Since I'm running without a database I'd like some sort of other record.
<?php
$to = "me#me.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "New Message";
$fields = array();
$fields{"first_name"} = "first_name";
$fields{"last_name"} = "last_name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"hospital"} = "hospital";
$fields{"title"} = "title";
$body = "Here is what was sent:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$send = mail($to, $subject, $body, $headers);
?>
How do I save the results to a text file in addition to email? I've seen examples on how to do text but not in addition to email.
you should check the file functions :
http://php.net/manual/en/function.fread.php
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fwrite.php
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.file-put-contents.php
An example of what you could do :
$filePath = 'records.txt';
file_put_contents($filePath, "\nYour text here...", FILE_APPEND);
Should be something like this:
//Email code here...
if(!$file = fopen('records.txt', 'a+')) {
echo 'Could not write to file.';
exit;
}
$content = "Email sent on: " . time() . PHP_EOL . "***" . PHP_EOL . $body . PHP_EOL . "***" . PHP_EOL;
if(fwrite($file, $content) === false) {
echo 'Could not write to file.';
exit;
}
fclose($file);
For more see examples at: http://php.net/fwrite
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am try to get what is problem but not find out.Please Help me.What is problem in this code.only mail not send message will show "pass".
This code is not send mail but message will show pass.
<?php
$name = $_POST['name'];
$employeestrength = $_POST['employeestrength'];
$companyweb = $_POST['companyweb'];
$contactpersonname = $_POST['contactpersonname'];
$contactnumber = $_POST['contactnumber'];
$emailaddress = $_POST['emailaddress'];
$cityandstste = $_POST['cityandstste'];
$massege = $_POST['massege'];
$captch = $_POST['captch'];
$to = "balvant.alpha#gmail.com";
$subject = "E-mail from HRMSsystem.com for new requirement";
$message = "<br>";
$message .= "<b>Name :</b> $name <br>";
$message .= "<b>Email :</b> $employeestrength <br>";
$message .= "<b>organization :</b> $companyweb<br>";
$message .= "<b>Phone Number:</b> $contactpersonname <br>";
$message .= "<b>Comapny :</b> $contactnumber <br>";
$message .= "<b>City :</b> $emailaddress <br>";
$message .= "<b>Description :</b> $cityandstste <br>";
$message .= "<b>Massege :</b> $massege <br>";
$message .= "<b>captch :</b> $captch <br>";
$message .= "<br>";
$header = "From:no-reply#hrmssystem.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$body = "We have received the following information:\n\n";
$headers2 = "From:no-reply#hrmssystem.com";
$subject2 = "Request to get listed into HRMSsystem.com";
$message2 = "Dear, Thank you for request and details to list into HRMSsystem.com directory. Our team will verify and list your details into directory as soon as possible. Kindly contact team#hrmssystem.com for more details or questions.";
$retval = mail($to,$subject,$message,$header);
mail($to, $subject, $body);
$send2 = mail($emailaddress, $subject2, $message2,$headers2);
if( $retval == true )
{
//echo "pass";
header('Location: http://localhost:8080/caofficeautomation/');
}
else{
echo "Message could not be sent..."; }
?>
Thanks
Please Help Me for short Out this problem.
Also, please check this answer (if you trying to send mail from localhost).
php send mail from localhost
I know the title is weird I cant for the life of me phrase it well lol.
I have done searches with multiple ways of phrasing the question and nothing shows up for this.
I have the email scripting working on the website im building and its fantastic! but when i edited the mail code to add extra message lines its made the sequence go wrong.
here is the code im using for the email message area:
<?php
require_once "Mail.php";
// load the variables form address bar
$name = $_REQUEST["name"];
$subject = 'Customer Feedback';
$message = $_REQUEST["message"];
$from = $_REQUEST["from"];
$compname = $_REQUEST["companyName"];
$ph = $_REQUEST["phone"];
$acp = $_REQUEST['allowCommentPublish'];
$marketing = $_REQUEST['incmarketing'];
$verif_box = $_REQUEST["verif_box"];
// Checking the check boxes and marking as appropriate
if(isset($_POST['allowCommentPublish']))
{
$acp = 'Yes';
}
else
{
$acp = 'No';
}
if(isset($_POST['incmarketing']))
{
$marketing = 'Yes';
}
else
{
$marketing = 'No';
}
// Optional data checker
if($compname == '')
{
$compname = 'N/A';
}
if($ph == '')
{
$ph = 'N/A';
}
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$message = stripslashes($message);
$subject = stripslashes($subject);
$acp = stripcslashes($acp);
$marketing = stripcslashes($marketing);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon'])
{
// if verification code was correct send the message and show this page
$ToEmail = "email#email.com";
$message = "Name: ".$name."\n".$message;
$message = "From: ".$from."\n".$message;
$message = "Comments: ".$message."\n".$message;
$message = "Allow feedback to be Published: ".$acp."\n".$message;
$message = "[ OPTIONAL DATA ]"."\n".$message;
$message = "Company Name: ".$compname."\n".$message;
$message = "Phone Number: ".$ph."\n".$message;
$message = "Allow extra Marketing? ".$marketing."\n".$message;
$headers = array ('From' => $from,
'To' => $ToEmail,
'Subject' => 'Feedback: '.$subject);
$smtp = Mail::factory('smtp', array ('host' => 'smtp.vic.exemail.com.au', 'auth' => false));
$mail = $smtp->send($ToEmail, $headers, $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
header("Location: /feedback_sent.php");
exit;
}
else
{
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
In my mind this should spit out the message body as this:
Name: name here
From: Email address
Comments: Message here
Allow feedback to be published: response
[ OPTIONAL DATA ]
Company Name: Company
Phone Number: Phone
Allow extra Marketing:
This should be how its seen in the email right?
What im actually getting is this:
Allow feedback to be Published: response
[ OPTIONAL DATA ]
Company Name: company
Phone Number: phone
Allow extra Marketing? Response
From: Email address
Name: name here
Comments: Message here
Is this normal? or have i inadvertently snuffed it somewhere along the lines and its messing with my head as payment?
Thanks for any help on this.
EDIT: Updated code.
<?php
// -----------------------------------------
// The Web Help .com
// -----------------------------------------
// remember to replace your#email.com with your own email address lower in this code.
require_once "Mail.php";
// load the variables form address bar
$name = $_REQUEST["name"];
$subject = 'Customer Feedback';
$comment = $_REQUEST["message"];
$from = $_REQUEST["from"];
$compname = $_REQUEST["companyName"];
$ph = $_REQUEST["phone"];
$acp = $_REQUEST['allowCommentPublish'];
$marketing = $_REQUEST['incmarketing'];
$verif_box = $_REQUEST["verif_box"];
// Checking the check boxes and marking as appropriate
if(isset($_POST['allowCommentPublish']))
{
$acp = 'Yes';
}
else
{
$acp = 'No';
}
if(isset($_POST['incmarketing']))
{
$marketing = 'Yes';
}
else
{
$marketing = 'No';
}
// Optional data checker
if($compname == '')
{
$compname = 'N/A';
}
if($ph == '')
{
$ph = 'N/A';
}
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name);
$comment = stripslashes($comment);
$subject = stripslashes($subject);
$acp = stripcslashes($acp);
$marketing = stripcslashes($marketing);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon'])
{
// if verification code was correct send the message and show this page
$ToEmail = "jim#digital2go.com.au";
$message = "Name: ".$name."\n".$message;
$message .= "From: ".$from."\n".$message;
$message .= "Comments: ".$comment."\n".$message;
$message .= "Allow feedback to be Published: ".$acp."\n".$message;
$message .= "[ OPTIONAL DATA ]"."\n".$message;
$message .= "Company Name: ".$compname."\n".$message;
$message .= "Phone Number: ".$ph."\n".$message;
$message .= "Allow extra Marketing? ".$marketing."\n".$message;
$headers = array ('From' => $from,
'To' => $ToEmail,
'Subject' => 'Feedback: '.$subject);
$smtp = Mail::factory('smtp', array ('host' => 'smtp.vic.exemail.com.au', 'auth' => false));
$mail = $smtp->send($ToEmail, $headers, $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
header("Location: /feedback_sent.php");
exit;
}
else
{
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
Make your message "continue" in the order you wish by doing this:
$message = "Name: ".$name."\n".$message;
$message .= "From: ".$from."\n".$message;
$message .= "Comments: ".$message."\n".$message;
$message .= "Allow feedback to be Published: ".$acp."\n".$message;
$message .= "[ OPTIONAL DATA ]"."\n".$message;
$message .= "Company Name: ".$compname."\n".$message;
$message .= "Phone Number: ".$ph."\n".$message;
$message .= "Allow extra Marketing? ".$marketing."\n".$message;
I am new to PHP not an expert by any means. Anyhow, I am building a PHP and HTML contact form and I am getting mixed up on the way to validate field input (trim, strip, htmlspecchars..). Anyways, here is my code, please go easy on me, I am a noob at this.
<?php
// define variables and set to empty values
$name = $email = $web = $telephone = $pages = $completion_date = $update_option = $hosting_option = $domain_option = $text = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$web = test_input($_POST["web"]);
$telephone = test_input($_POST["telephone"]);
$pages = test_input($_POST["pages"]);
$completion_date = test_input($_POST["completion_date"]);
$update_option = test_input($_POST["update_option"]);
$hosting_option = test_input($_POST["hosting_option"]);
$domain_option = test_input($_POST["domain_option"]);
$text = test_input($_POST["text"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$msg = $name . "\n";
$msg = $email . "\n";
$msg = $web . "\n";
$msg = $telephone . "\n";
$msg = $pages . "\n";
$msg = $completion_date . "\n";
$msg = $update_option . "\n";
$msg = $hosting_option . "\n";
$msg = $domain_option . "\n";
$msg = $text . "\n";
$recipient = "myemail#mydomain.com";
$subject = "Contact Has Been Made..";
$mailheaders = "MIME-Version: 1.0" . "\r\n";
$mailheaders = "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$mailheaders = "From: <myemail#mydomain.com>, Reply-To: <myemail#mydomain.com>" . "\r\n";
$mailheaders = "Cc: <$email>" . "\r\n";
mail($recipient, $subject, $msg, $mailheaders);
?>
It looks okay until the definition of $msg, you keep overwriting it.
Prepend the equals (=) signs after the first with a dot (.)
$msg = $name . "\n";
$msg .= $email . "\n";
$msg .= $web . "\n";
... etc
stripslashes and htmlspecialchars are unnecessary in this context. After all, you are not outputting any HTML that contain the POSTed values.
The problem with your form validation is that I could write anything in the email field, for example, and it would still validate. You should make a case-by-case validation for all the fields so, for example with the email field
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
// The email is valid
$email = $_POST['email'];
}
And so on. If you don't need them to be what they say the are, you can omit this. Other than that it looks okay.
Been struggling very much with only sending the fields that people fill in. It is a simple order form with text fields. But I want it so that only those fields that are field in get sent to my alert email. I tried the codes on the topic "PHP form: Success email with only filled fields" but my php script crashes when I do. Here is my code before I tried any modifying. This email fine only that it includes all fields I only want the one filled in. Please help I am a total noob in php. All help appreciated.
<?
$silver_name_badges = !empty($_POST['silver_name_badges']) ? ($_POST['silver_name_badges'])) : false;
$silver_name_badges = $_POST['silver_name_badges'];
$coffee_mug = $_POST['coffee_mug'];
$plastic_bag = $_POST['plastic_bag'];
$paper_bag = $_POST['paper_bag'];
$candy = $_POST['candy'];
$moist_towlette = $_POST['moist_towlette'];
$notepad_and_pen = $_POST['notepad_and_pen'];
$tuck_box = $_POST['tuck_box'];
$red_tie = $_POST['red_tie'];
$cap = $_POST['cap'];
$name = $_POST['Attention_To'];
$red_lanyard = $_POST['red_lanyard'];
$white_lanyard = $_POST['white_lanyard'];
$green_lanyard = $_POST['green_lanyard'];
$black_lanyard = $_POST['black_lanyard'];
$LC2415 = $_POST['LC2415'];
$FOM_2NTRI = $_POST['FOM_2NTRI'];
$ASTRO_165WMT = $_POST['ASTRO_165WMT'];
$FOL_AJ3015 = $_POST['FOL_AJ3015'];
$ACIES_NT = $_POST['ACIES_NT'];
// Please specify your Mail Server - Example: mail.yourdomain.com.
ini_set("SMTP","mail.ama-japan.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'ravila#ama.com');
// Set parameters of the email
$to = "joe#ama.com";
$subject = "Ama Promo Items Ordered";
$from = " jurt#ama.com";
$headers = "From: $from";
$message = "";
$message .= "Order has been placed. Attn to: $name .\n
Items:\n";
if($Phone)
$Body .= "Phone: "; $Body .= $Phone; $Body .= "\n";
$message .="
Silver Name Badges: $silver_name_badges\n
Coffee Mug: $coffee_mug\n
Plastic Literature Bag: $plastic_bag\n
Amada Bag: $paper_bag\n
Candy: $candy\n
Notepad & Pen: $notepad_and_pen\n
Tuck Box: $tuck_box\n
Amada Tie: $red_tie\n
Candy: $candy\n
Moist Towlette: $moist_towlette\n
Cap: $cap\n
Lanyard:
Red - $red_lanyard
White - $white_lanyard
Green - $green_lanyard
Black - $black_lanyard\n
Rock Glass:
LC2415 - $LC2415
FOM 2NTRI - $FOM_2NTRI
ASTRO 165WMT - $ASTRO_165WMT
FOL AJ3015 - $FOL_AJ3015
ACIES NT - $ACIES_NT";
// Mail function that sends the email.
mail($to,$subject,$message,$headers);
header('Location: thank-you.html');
?>
The errors might be related to unset POST variables.
How about a loop? It will only use what is sent over.
$message = "";
$message .= "Order has been placed. Attn to: $name .\n
Items:\n";
foreach ($_POST as $fieldName => $fieldValue){
if (!empty($fieldValue))
$message .= " ". str_replace('_',' ',$fieldName) .": $fieldValue\n";
}
OK I added this and worked great. How the email comes back with the field name with underscores. How to send without underscores?:
<?php
//$silver_name_badges = !empty($_POST['silver_name_badges']) ? ($_POST['silver_name_badges'])) : false;
$SilverNameBadges = $_POST['Silver Name Badges'];
$CoffeeMug = $_POST['coffee_mug'];
$PlasticLiteratureBag = $_POST['plastic_bag'];
$paper_bag = $_POST['paper_bag'];
$candy = $_POST['candy'];
$moist_towlette = $_POST['moist_towlette'];
$notepad_and_pen = $_POST['notepad_and_pen'];
$tuck_box = $_POST['tuck_box'];
$red_tie = $_POST['red_tie'];
$cap = $_POST['cap'];
$name = $_POST['Attention_To'];
$red_lanyard = $_POST['red_lanyard'];
$white_lanyard = $_POST['white_lanyard'];
$green_lanyard = $_POST['green_lanyard'];
$black_lanyard = $_POST['black_lanyard'];
$LC2415 = $_POST['LC2415'];
$FOM_2NTRI = $_POST['FOM_2NTRI'];
$ASTRO_165WMT = $_POST['ASTRO_165WMT'];
$FOL_AJ3015 = $_POST['FOL_AJ3015'];
$ACIES_NT = $_POST['ACIES_NT'];
// Please specify your Mail Server - Example: mail.yourdomain.com.
ini_set("SMTP","mail.amada-america.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'ravila#amada.com');
// Set parameters of the email
$to = "ravila#amada.com";
$subject = "Amada Promo Items Ordered";
$from = " nostrowski#amada.com";
$headers = "From: $from";
$message = "";
$message .= "Order has been placed. Attn to: $name .\n
Items:\n";
foreach ($_POST as $fieldName => $fieldValue){
if (!empty($fieldValue))
$message .= " $fieldName: $fieldValue\n";
}
// Mail function that sends the email.
mail($to,$subject,$message,$headers);
header('Location: thank-you.html');
?>