PHP: Line Breaks on email sending not working - php

The following PHP code works perfectly, but it is not doing line breaks for some reason.
PHP:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: '".$title."' <".$store_email."> \n";
$subject = "New Payment Received";
//MESSAGE
$message = "New payment was successfully recieved through paypal payment terminal:";
$message .= "\r\n\nFrom ".$paypal->pp_data['payer_email'];
$message .= "\r\nPaid: ".$paypal->pp_data['payment_gross']." ".$paypal->pp_data['mc_currency'];
$message .= "\r\nDate: ".date('d/m/Y');
$message .= "\r\nTime: ".date('g:i A');
mail($admin_email,$subject,$message,$headers);
Any wonder what's wrong? Thanks in advance.

You're sending HTML e-mail. Line breaks have no meaning in HTML, you'll need <br /> tags.

The direct answer ceejayoz gives is correct and to the point in that the html element <br> is needed because it is a html email.
The bigger issue is that not all email is readable in html (example: user doesn't allow html emails). Anyone sending email should send it in 2 parts. One being a html formatted message and the other "alternative" in plain text. In that way the recipient will be able to read the email regardless of email reader.
The \r\n line break works in plain text alternative part and in html<br> or other elements as needed to format.
Doing this will avoid the next question. Recipients are complaining my emails are blank.

Related

The e-mail header from the online form is displayed incorrectly

I have a questionnaire form on the web. After filling in this form, I send it to me by email, using the PHP function mail(). The form body and the data it contains, including the private message are displayed correctly on gmail.com. The problem, however, occurs in the header of the email itself. Some characters are displayed incorrectly.
Here is a sample header:
$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;
Required display of email subject:
Nový dotaz -- námět, od Fořt Petr <p.fort1990#gmail.com>
Simultaneous displaying of the subject:
Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990#gmail.com>
The squared times symbol is more like a rectangle.
Is anything wrong? Or where should I look for a mistake?
I'm not sure \r\n works on all platforms
see : Which line break in php mail header, \r\n or \n?
instead
("xxx\r\n\yyy");
use
Header('xxx');
Header('yyy');
or use PHP_EOL, not "\r\n"
Problem solved. My hosting provider uses different character encoding for the headers - I can't explain why, but the following php function will do it all.
function recode_to_utf8 ($text, $encoding = "utf-8")
{
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
And now all you have to do is send an email using the mail () method in combination with the method defined above recode_to_utf8(). Like this:
mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));
I hope it helps others if they have the same problem as me.

php html mail not working on cpanel server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a code to send html mail with php on cpanel but doesn't send without any errors. I have tested plain text mail and that is working fine, but once I add the header like below, it doesn't work.
//send html mail
ini_set('sendmail_from', 'info#toludimaokojie.com');
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "Content-type: text/html\r\n";
$html = '<html>
<body>
<h2>Result Analysis for Test With Reference Number:'.$reference.'</h2>
<br/>
<p><b>Personality: '.$personality_mail.'</b></p>
<br/>
<p><b>MBQ Score: '.$resultTotal.'</b></p>
<br/>
<ul>'.$analyseData.'</ul>
<pre>Mail Sent on '.date("l, F Y H:i:sa").'</pre>
</body>
</html>';
mail("olaegbesamuel#gmail.com", "MBQ TEST ANALYSIS", $html, $headers);
This doesn't work. Please help, i guess am doing everything correctly here. I have tested without html and confirmed that mail is working
You have defined $headers with all mandatory email parameters but look at your $header variable, there are two things need to be changed,
1) You have used $header and $headers both so use either of them and append them to one variable.
2) your 2nd line of $header variable is missing . to append the previous header values, so the corrected code should be:
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
As you missed . operator in 2nd line, it will remove From header and so it would not be sending email.
Try after changing these lines.

Embed Image issue strip slash

I have a script which inputs form data then generates an email to the user who completes the form. The email goes to the email recipient all fine, but the image does not show.( I get a broken link icon in Mail).
I looked at the source code of the email when it arrived and it appears the backslashes where before the double quotes. How do I get this to work?
Email source code:
<img src=\"https://amb.cbussuper.com.au/registration-test/images/sig.png\” /><br><br>
PHP Code:
$message = "<html><body>";
$message .= "<b>Thank you for registering for the meeting</b><br>";
$message .= "- Day: 28 October 2014<br>";
$message .= "- Time: 7pm<br><br>";
$message .= "If you have any questions about the meeting please email email#address.com.au<br><br>";
$message .= "Please do not reply - this is an automatically generated email.<br><br>";
$message = stripcslashes($message);
$message .= '<img src="https://website.com.au/registration/images/sig.png" /><br><br>';
$message .= "</body></html>";
This is quite simple:
https://website.com.au/registration/images/sig.png
Not Found
The requested URL /registration/images/sig.png was not found on this server.
Apache Server at website.com.au Port 443
Please upload the file which you want to refer as an image ...

Trying to insert linebreaks within php variable

Please bear with me as I'm still very new to this. I'm simply trying to ensure the email message body below has linebreaks where I put <br/>. However when I run the script the message body displays exactly as it is in the script even though it works perfectly in the editor I used to compose this email at Stackoverflow.
I realize its something very small and subtle. /n also didnt seem to work. Thanks for your help! I'm not sure i understand how to use nl2br in my particular context. Again thanks for your patience and help.
$subject=" You're exclusive guide is only a click away!";
$message="Thanks for subscribing .$name! <br/> Click below to confirm your email and email and access your guide <br/> http://acmecorp.net/guide <br/> Acmecorp.net <br />Phone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
mail( $email, $subject, $message,$headers );`
Substitute <br/> with "\n":
$subject=" You're exclusive guide is only a click away!";
$message="Thanks for subscribing .$name! \n Click below to confirm your email and email and access your guide \n http://acmecorp.net/guide \n Acmecorp.net \nPhone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
mail( $email, $subject, $message,$headers );`
You want to use HTML tags in a mail obviously. So, you have to send your mail in HTML, by adding a content type revelant in the headers.
Just before your mail() call, add $headers .= "Content-type: text/html\r\n"; to have something like this:
$subject = " You're exclusive guide is only a click away!";
$message = "Thanks for subscribing .$name! <br/> Click below to confirm your email and email and access your guide <br/> http://acmecorp.net/guide <br/> Acmecorp.net <br />Phone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
$headers .= "Content-type: text/html\r\n";
mail($email, $subject, $message, $headers);

Formatting Php Form Variable

A user fills out a form which is sent to my email via the variables their data is read into.
I want to have headers above each users entry, and have these appear bold in the email.
The email should read:
OCCUPATION:
Data the user entered.
I have tried
$Occupationheader = "<strong>"."OCCUPATION"."</strong>"."\n\n" ;
and
$Occupationheader = "<strong>OCCUPATION:<strong>\n\n";
The data is sent to the email, as follows: (this works, but want to format the headings in bold).
mail( "myemailaddress", "subjectmatter",
$Occupationheader.$Occupation);
Any ideas?
Thanks guys.
$mailContent = "<html>
<body>
<strong>HEADER</strong>
<br/>message
</body>
</html>";
NOTE: This is provided as an additional answer.
Without seeing full source code, am providing my (additional) answer below.
The following, need to be present in your code, in order to send out Emails in HTML format:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
As per the PHP manual on the subject: http://php.net/manual/en/function.mail.php

Categories