HEREDOC text and variables formating issue - php

I created a PHP script that collects variables from HTML and converts them to PHP variables. Then takes those variables and inserts them into a HEREDOC string and finally sends an email to a predefined person. I'm having an issue getting the text to format with a carriage return after each variable. So that all the text in the email is left side formatted.
What I am getting is this:
SFARC Membership Application Date: February 19th. 2019 First Name: XXXXXX Last Name: XXXXXXX Nick Name: XXXXXXX
Here is portion of my code that handles the text string:
// Generate application in a message
$message = <<<EOT
SFARC Membership Application
Date: $App_Date
First Name: $First_Name
Last Name: $Last_Name
Nick Name: $Nick_Name
Address: $Address
City/Town: $City_town
Zip Code: $Zip_code
Email: $Email
Home Phone: $Home_phone
Cell Phone: $Cel_phone
Callsign: $Call_sign
ARRL Member: $Arrl_member
Membership Type: $Membership_type
Membership Term: $Membership_term year(s)
Payment Method: $Payment_method
Membership Dues: $Membership_dues
EOT;
// Sending email
if(mail($to, $subject, $message, $headers )){
echo 'Your membership application has been successfully submitted.';
} else{
echo 'Unable to submit your membership application. Please try
again.';
};
?>
Godaddy is who is hosting my website. Is that the issue? I watched several youtube videos and I have no idea what I am missing? Is there a better way to code to achieve the results I am looking for?
Thanks in advance.

I think your message is send by HTML reason. You can confirm it by content type in the header of email. Or add $message = nl2br($message); before send and try again.

You probably tried this, but I want to double check: did you try putting \n or \r at the end of lines?

Related

Php email form not sending email from web email form

I am trying to troubleshoot this form. It is not sending reservation requests from the form on the website. Despite showing a message that the form was sent.
I tried editing email and the headers.
<?
//print_r($_POST);
$to = “email#emaildomain.com, {$posting['email']}";
function msg($text){
echo "
<script type='text/javascript'>
alert('".$text."');
top.location.href = 'http://www.aribbq.com';
</script>
";
exit;
}
function error($text){
echo "
<script type='text/javascript'>
alert('".$text."');
history.go(-1);
</script>
";
exit;
}
if (!$_POST[date]) {error('Please, insert Date.');}
if (!$_POST[time]) {error('Please, insert Time.');}
if (!$_POST[party]) {error('Please, insert Party.');}
if (!$_POST[reservation_name]) {error('Please, insert Name.');}
if (!$_POST[reservation_email]) {error('Please, insert Email.');}
if (!$_POST[reservation_phone]) {error('Please, insert Phone.');}
if(isset($_POST['submit'])){
// then send the form to your email
//$from = ('Reservation from AriBBQ.com'); // sender
$mailheaders = "From: contact#aribbq.com" . "\r\n"; // . "CC:
design#youremail.com"
$mailheaders .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$subject = "AriBBQ.com Online Reservation";
$body = "\n Contact Name: ".$_POST[reservation_name]." \r\n\n";
//
$body .= " Email: ".$_POST[reservation_email]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$body .= " Book a table \r\n\n
Date: ".$_POST[date]." \r\n\n
Time: ".$_POST[time]." \r\n\n
Party: ".$_POST[party]." \r\n\n
Contact Details \r\n\n
Name: ".$_POST[reservation_name]." \r\n\n
Email: ".$_POST[reservation_email]." \r\n\n
Phone: ".$_POST[reservation_phone]." \r\n\n
Message: ".$_POST[reservation_message]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$result = mail($to , $from , $subject , $body , $mailheaders);
if($result) {msg('Thank you, your reservation has been sent. We
will send you a confirmation text or call in person.');} //
else{error('Sending mail is failed. Please try again');} //
} else {
error('No submitted. Please try again');
}
?>
You see the form online at http://aribbq.com/. Click on reservations. Once the email is received, we want to be able to reply to the sender's email address.
Alright, essentially, you need to turn on error reporting because your script threw about 20 errors at me which you would see with error reporting on. As my comment above said, add error_reporting(E_ALL); to the top of your script while you debug.
The issues I came across are as follows:
Parse error: syntax error, unexpected '#' in /mail.php on line 4 caused by an incorrect double quote character, not " but “. Subtle, but problematic.
Next up, Multiple or malformed newlines found in additional_header in /mail.php because as of PHP 5.5.2, a bug was fixed to prevent mail header injection, so all of your \n\n within the $mailheaders should be removed, I recommend appending PHP_EOL to the end of each line instead.
You have your $from variable included in the mail() call, this presents 2 issues. One, the mail() function does not have a from parameter, you include it within the headers. Two - your variable is actually commented out.
As I mentioned in the comment above, again, your email address variable to send to is typed as $posting['email']', and $posting['Email'] within $mailheaders. The problem here is $posting doesn't exist. Secondly, your form, which you should include the HTML for in future questions for self-contained examples for people to more easily help you (see https://stackoverflow.com/help/how-to-ask), doesn't post email at all, it posts reservation_email.
Finally, the majority of your $_POST references do not include quotes so PHP doesn't know what to do with the words in between the square brackets. $_POST[date] should be $_POST['date'], for example.
I've made all the above changes and managed to successfully email myself with the script and email form provided, the only thing that I didn't look at was your msg() which didn't show me a success message. I did, however, put an echo statement before this function call which printed out fine.
I hope this helps you get your script up and running, good luck and remember, error_reporting(); is your friend!

How can I use $POST in PHP to automatically collect data from HTML5 inputs and send through e-mail?

I have a questionnaire with tons of input fields. So far I started a PHP script to send it to my email which goes as follows.
<?php
$First_Name= $_POST['First_Name'];
$Last_Name= $_POST['Last_Name'];
$E_Mail= $_POST['E_Mail'];
$FB_Name= $_POST['FB_Name'];
$Twitter_Name= $_POST['Twitter_Name'];
$Country= $_POST['Country'];
$State_Province= $_POST['State_Province'];
$City= $_POST['City'];
$Phone_01= $_POST['Phone_01'];
$Phone_02= $_POST['Phone_02'];
$Phone_03= $_POST['Phone_03'];
$How_did_you_hear_about_my_Company= $_POST['How_did_you_hear_about_my_Company'];
$Operating_your_Business= $_POST['Operating_your_Business'];
$Name_for_your_Business= $_POST['Name_for_your_Business'];
$Whats_the_name_of_your_Business= $_POST['Whats_the_name_of_your_Business'];
$What_type_of_Business= $_POST['What_type_of_Business'];
$Type_of_Business_you_run= $_POST['Type_of_Business_you_run'];
$How_long_you_been_building= $_POST['How_long_you_been_building'];
$How_long_you_been_in_business= $_POST['How_long_you_been_in_business'];
$What_do_you_plan_to_offer= $_POST['What_do_you_plan_to_offer'];
$What_do_you_offer= $_POST['What_do_you_offer'];
$List_all_products_you_offer['List_all_products_you_offer'];
$from = 'From: Your_New_Client';
$to = 'Optiqvision#gmail.com';
$subject = 'A New Questionnaire';
$body = "Name: $First_Name $Last_Name
\n E-Mail: $E_Mail
\n FB Name: $FB_Name
\n Twitter Name: $Twitter_Name
\n Country: $Country
\n State/Province: $State_Province
\n City: $City
\n Phone Number: $Phone_01 $Phone_02 $Phone_03
\n How did you hear about my Company?:\n\n $How_did_you_hear_about_my_Company
\n Where are you in terms of opperating your Business?: $Operating_your_Business
\n Have you come up with a name for your Business?: $Name_for_your_Business
\n What's the name of your Business?: $Whats_the_name_of_your_Business
\n What type of Business will you be running?: $What_type_of_Business
\n What type of Business do you run?: $Type_of_Business_you_run
\n How long have you been Building your Business?: $How_long_you_been_building
\n How long have you been in Business?: $How_long_you_been_in_business
\n What do you plan to offer?: $What_do_you_plan_to_offer
\n What do you offer?: $What_do_you_offer
\n List of products: $List_all_products_you_offer
";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
This works properly, but I want to know how I can tell the PHP to automatically collect all the data without me having to go one by one with each element (there's a lot more than what's represented above).
I came across a script that does this for storing to an SQL database, but not one for email. I tried using the one for SQL but was running into issues because of how many columns it creates.
Aside from this I have another task to tackle with allowing users to add fields which will vary from user to user, so I need the $POST method to be flexible so it can send the results. With the way I have it coded so far, there's no way for extra fields to be posted when the user adds them.
I searched through the site and so far this is the only thing I've come across that relates to this issue, but don't really understand what's being said. PHP: Possible to automatically get all POSTed data?
If that is indeed the answer to what I'm trying to do could someone please break it down a little more and tell me how it works?
You can try like this:
$body="";
foreach ($_POST as $key => $value) {
$body.= str_replace("_"," ",$key).": ". $value."\n";
}
provided that $key holds the descriptive information which you need with words separated by underscore (_) as in your question is.

Get Contents from Text File and Replace Keywords PHP

I have a form. I'm using "post" method that sends and emails to multiple people that registered. Then the $body of the email is based on a template. No database, no classes, simple form. Yes I read up about this already they're all there I just couldn't put it together, related to this case.
The text "email_template.txt" should have something like:
Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#
Would look like this upon processing by PHP
Hello **John Doe**. Thank you registering! Your registered email is **example#example.com**.
On my php I have something like:
<?php
//form and validation
$firstname = "John"; // inputed name on the form, let say
$lastname = "Doe"; // inputed last name
$email = "example"; // inputed email
//email message
$body = ... some type of a get_file_content....
mail($_POST['email'], 'Party Invitation', $body, 'From: admin#admin.com');
?>
Where $body is the email message to the registrants submitted via this PHP form.
sprintf will work good
your template could be:
Hello %s %s. Thank you for registering! Your registered email is %s
$body = sprintf($fileContents,$firstname,$lastname,$email);
or str_replace(), pretty much a find replace.
$body = "Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#";
$body = str_replace("#firstname#",$firstname,$body);
$body = str_replace("#lastname#",$lastname,$body);
$body = str_replace("#email#",$email,$body);

Sending Plain Text Emails

I am trying to send plain text emails to a company who will be processing them for me. I am using the Swift Mailer library and as far as I can tell - I have no problems. But the company that I am sending the messages to says that they are coming into them with all of the data on a single line. When I send myself an email using this script it looks perfect - each piece of data is on a separate line.
Here is the script I am using. It is very simple.
<?php
$to="";
$bcc="";
$subject = "Quote" ;
$domain = $_SERVER['HTTP_HOST'];
//Message
$message = "Domestic Quote Request\n";
$message .= "Full Name: $_POST[name]\n";
$message .= "Email: $_POST[email]\n";
$message .= "Phone: $_POST[phone]\n";
$message .= "Pickup Date: $_POST[shipdate]\n";
$message .= "Pickup City or Zip: $_POST[pickupzip]\n";
$message .= "Drop Off City or Zip: $_POST[dropoffzip]\n";
$message .= "Year: $_POST[vehicleyear]\n";
$message .= "Make: $_POST[vehiclemake]\n";
$message .= "Model: $_POST[vehiclemodel]\n";
$message .= "Carrier Type: $_POST[carriertype]\n";
$message .= "This Quote is from $domain";
require_once 'lib/swift_required.php';
// Create the Transport
// Mail
$transport = Swift_MailTransport::newInstance();
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$the_message = Swift_Message::newInstance()
// Give the message a subject
->setSubject($subject)
// Set the From address with an associative array
->setFrom($_POST['email'])
// Set the To addresses with an associative array
->setTo(array($to))
->setEncoder(Swift_Encoding::get8BitEncoding())
// Give it a body
->setBody($message, 'text/plain', 'us-ascii');
//Check is BCC Field is emtpy.
if ( !empty($bcc) )
{ //The BCC Field is not empty so set it.
$the_message->setBcc(explode(',', $bcc));
}
// Send the message
$result = $mailer->send($the_message);
}
header("location:index.php?s=1");
?>
Here is an example of what the company is saying they are receiving:
Domestic Quote Request Full Name: Test Email: someone#somewhere.com Phone: 555-555-5555 Pickup Date: 07/02/2012 Pickup City or Zip: 12938 Drop Off City or Zip: 23981 Year: 2009 Make: Audi Model: A4 Carrier Type: enclosed carrier This Quote is from awebsite.com
And this is what I receive when I email myself.
Domestic Quote Request
Full Name: Test
Email: someone#somewhere.com
Phone: 555-555-5555
Pickup Date: 07/02/2012
Pickup City or Zip: 12938
Drop Off City or Zip: 23981
Year: 2009
Make: Audi
Model: M4
Carrier Type: enclosed carrier
This Quote is from awebsite.com
The company said that they believed that the lead was being sent as free text instead of plain text. Is there such a thing? I have never heard of free text.
Thank you in advance for the help.
The only thing I can think of is that the web client they are viewing the email in parses emails in HTML. So instead of \n try <br /> maybe.
Email body requires Microsoft \r\n line endings.
The headers require Linux \n line endings.

php syntax for new line is not working

I am having a rather simple question
can anyone tell me why this is not displaying each variable on a new line, well except for
the <br>.
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =
"##$curtime##<br>".
"PayPal Transaction Information...\n".
"Txn Id: ".$ppInfo["txn_id"]."\n".
"Txn Type: ".$ppInfo["txn_type"]."\n".
"Item Number: ".$ppInfo["item_number"]."\n".
"Payment Date: ".$ppInfo["payment_date"]."\n".
"Payment Type: ".$ppInfo["payment_type"]."\n".
"Payment Status: ".$ppInfo["payment_status"]."\n".
"Currency: ".$ppInfo["mc_currency"]."\n".
"Payment Gross: ".$ppInfo["payment_gross"]."\n".
"Payment Fee: ".$ppInfo["payment_fee"]."\n".
"Payer Email: ".$ppInfo["payer_email"]."\n".
"Payer Id: ".$ppInfo["payer_id"]."\n".
"Payer Name: ".$ppInfo["first_name"]." ".$ppInfo["last_name"]."\n".
"Payer Status: ".$ppInfo["payer_status"]."\n".
"Country: ".$ppInfo["residence_country"]."\n".
"Business: ".$ppInfo["business"]."\n".
"Receiver Email: ".$ppInfo["receiver_email"]."\n".
"Receiver Id: ".$ppInfo["receiver_id"]."\n";
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;
thanks Richard
Carriage returns have no effect if you're viewing this output as HTML, so try turning them into <br> tags with nl2br...
echo nl2br($oremarks);
Because you are outputting the result in the browser window, try "<br />" instead of "\n".
Presumably because you are generating HTML source code from PHP, and not plain text.
In HTML, a new line is treated like any other whitespace. You need a <br> element or something that is display: block (or similar) to trigger a line break.
Try alternating the Double quotes for your array values, use single quotes instead
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =
"##$curtime##<br>".
"PayPal Transaction Information...\n".
"Txn Id: ".$ppInfo['txn_id']."\n".
"Txn Type: ".$ppInfo['txn_type']."\n".
"Item Number: ".$ppInfo['item_number']."\n".
"Payment Date: ".$ppInfo['payment_date']."\n".
"Payment Type: ".$ppInfo['payment_type']."\n".
"Payment Status: ".$ppInfo['payment_status']."\n".
"Currency: ".$ppInfo['mc_currency']."\n".
"Payment Gross: ".$ppInfo['payment_gross']."\n".
"Payment Fee: ".$ppInfo['payment_fee']."\n".
"Payer Email: ".$ppInfo['payer_email']."\n".
"Payer Id: ".$ppInfo['payer_id']."\n".
"Payer Name: ".$ppInfo['first_name']." ".$ppInfo['last_name']."\n".
"Payer Status: ".$ppInfo['payer_status']."\n".
"Country: ".$ppInfo['residence_country']."\n".
"Business: ".$ppInfo['business']."\n".
"Receiver Email: ".$ppInfo['receiver_email']."\n".
"Receiver Id: ".$ppInfo['receiver_id']."\n";
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;
But I would recommend using a HEREDOC instead of concatenating a string
$curtime = gmdate("d/m/Y H:i:s");
//capture the PayPal returned information as order remarks
$oremarks =<<<OREMARKS
##$curtime##
PayPal Transaction Information...
Txn Id: $ppInfo['txn_id']
Txn Type: $ppInfo['txn_type']
Item Number: $ppInfo['item_number']
Payment Date: $ppInfo['payment_date']
Payment Type: $ppInfo['payment_type']
Payment Status: $ppInfo['payment_status']
Currency: $ppInfo['mc_currency']
Payment Gross: $ppInfo['payment_gross']
Payment Fee: $ppInfo['payment_fee']
Payer Email: $ppInfo['payer_email']
Payer Id: $ppInfo['payer_id']
Payer Name: $ppInfo['first_name'] $ppInfo['last_name']
Payer Status: $ppInfo['payer_status']
Country: $ppInfo['residence_country']
Business: $ppInfo['business']
Receiver Email: $ppInfo['receiver_email']
Receiver Id: $ppInfo['receiver_id']
OREMARKS;
//Update database using $orderno, set status to Paid
//Send confirmation email to buyer and notification email to merchant
//Redirect to thankyou page
echo $oremarks;
In html, newlines never go to line. You have to put <br> in your source.
Notice that php can also works independently from http server as a command line utility and does not necessarily generate html.
Thus if you set your content type in web server to plain/text instead of html using header("Content-type: plain/text"); at the beginning of your file your text would go to line as you expected.
\n only shows a newline in the source. is the "newline" character for HTML.
The '\n' is just creating a new line in the html code, it is not creating a new line that is visible. You need to use html to get the new line to be visible. You can use the the html break <br> or you can make each line a paragraph <p> your text... </p> or you can use a list:
<ul>
<li> your text... </li>
<li> next item... </li>
<li> more stuff.. </li>
</ul>
Try putting in the following:
<?php
echo "<pre>";
.
.
.
?>

Categories