Sending Plain Text Emails - php

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.

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!

HEREDOC text and variables formating issue

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?

my variable is not echoing <br> tag when it gets to the mail function

I am creating my own contact form and sending all the inputs through variable to the mail function
But there is one error in it......my code is like this in the below...
$from = $_POST["from"];
// sender
$name = $_POST["name"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
$phone = $_POST["phone"];
$mytour = $_POST["select1"];
$howmany = $_POST["select2"];
$arrival = $_POST["arrival"];
$departure = $_POST["departure"];
$accommodation = $_POST["select3"];
$company = $_POST["company"];
// send mail
$messagecontent = "Name:$name , " . "Email:$from ," . "Nature of Enquiry:$subject ," . "Message:$message ," . "Phone No:$phone, " . "My Tour Wish List: $mytour, " . "How many days do you have available:$howmany, " . "Arrival Date:$arrival ," . "Departure Date:$departure ," . "My Preferred Accommodation:$accommodation, " . "Company Name:$company ,";
// $messagewithbreak=str_replace(',',', <br/>;',$messagecontent); // code replaces all your commas with , and a <br> tag
mail("abc#gmail.com", " Contact form Filled", "$messagewithbreak", "From: $from\n");
echo "Thank you for sending us feedback";
}
But when I receive it in the my email...there is not break tag <br> tag...and it receives as....
Name: , Email:abc#gmail.com ,Nature of Enquiry:none ,Message:none ,Phone No:none,My Tour Wish List: Heritage Tour of Kells,How many days do you have available:Half a Day, Arrival Date:2014-09-10 ,Departure Date:2014-09-10 ,My Preferred Accommodation:Hostel,Company Name:none
But I want it to view proper in the email.... as that is not the professional way... to see the email moreover it is difficult to read the email like this.....
what should I do please help me on it....
I will really appreciate your help.
Please answer my question nothing is helping me out here.I have tried..the methods given to me by jenz and rakesh but still the form when receive in the email shows as the paragraph without line breaks....it is getting frustrating
why you concat message string again and again. Also remove double quotes from variables in mail()
$messagecontent="Name:$name , Email:$from , Nature of Enquiry:$subject , Message:$message , Phone No:$phone, My Tour Wish List: $mytour, How many days do you have available:$howmany, Arrival Date:$arrival , Departure Date:$departure , My Preferred Accommodation:$accommodation, Company Name:$company ,";
$messagewithbreak=str_replace(',',',<br>',$messagecontent);
mail("abc#gmail.com"," Contact form Filled", $messagewithbreak, $from);
Properly add formatting to your message. Any HTML tags placed inside double quotes will be converted to the tag when you echo it. So you just need to add <br> in proper places inside the message content. Also all PHP variables inside double quotes will get replaced by its value. So no need of appending each string with double quotes.
$messagecontent="Name:$name <br> Email:$from <br>Nature of Enquiry:$subject
<br>Message:$message<br>Phone No:$phone<br>
My Tour Wish List: $mytour<br>How many days do you have available:$howmany<br> Arrival Date:$arrival <br> Departure Date:$departure <br> My Preferred
Accommodation:$accommodation<br>Company Name:$company ";
To send HTML email you have to add headers which is optional by default where we pass Content-Type type declaration telling mail services that parse this email as HTML.
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
and pass $headers in mail() like,
mail("abc#gmail.com", " Contact form Filled", $messagecontent, $headers);
You are trying to use HTML in a plain text email. If all you want to do is have line breaks, then use \n instead of the br tag.
If you want to use HTML in an email you must add a content-type header. See the PHP mail docs for an example.
// send mail
$messagecontent = "Name:$name \n ";
$messagecontent.= "Email:$from \n";
$messagecontent.= "Nature of Enquiry:$subject \n";
............
...............
Set all the variables like that.

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

Bullet points in email information received from an filled in website form

I am building a website form page where a user enters in information which gets sent to my email once submitted. This appears to work ok, however, I want the information to be displayed in bullet points within the email once it has been sent.
Below shows the bit of code that sends all the information in one big chunk, but how do I add bullet points from each title i.e. company name, email, phone number etc? or even a line space is fine...
Code:
$to = 'bla#hotmail.com';
$subject = 'Quote';
$message = 'FROM: '.$companyname. '-' .$fullname. 'Email: '.$email. ' Phone Number: '.$phonenumber;
$headers = 'From: bla#hotmail.com' . "\r\n";
Expected Outcome:
Company Name:
Full Name:
Email:
Phone Number:
To send the list with formatting (or format text in general) you'll have to use HTML.
So, $headers will become:
$headers = 'From: bla#hotmail.com' . "\r\nContent-type: text/html\r\n";
to tell the client this mail is HTML and not plain text.
Then, the message will be
$message = <<<EOF
<html>
<body>
<ul>
<li>Company name: $companyname</li>
<li>Full name: $fullname </li>
</ul>
</body>
</html>
EOF;
See also "Example #4 Sending HTML email" here

Categories