Php html mail not displaying in html - php

I am trying to send an html email. Mail is working properly but not going as html email and it is just displaying ".$message.". The data is coming from the mysql database.
My script is as follows:
$to = $email;
$subject = $subject;
$message = "$message";
$htmlContent = '
<html>
<body>
".$message."
<br /><br />Regards<br /><b>TM</b>
</body>
</html>';
$headers = 'From: myemail#gmail.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
if(mail($to, $subject, $htmlContent, $headers))
echo "Mail Sent";
else
echo "Problem sending email";

You have mixed single and double quotes. Use only one type when concatenating strings.
$htmlContent = "
<html>
<body>
".$message."
<br /><br />Regards<br /><b>RainbowCTM</b>
</body>
</html>";

To complete previous answer, there is a huge difference between single and double quotes:
Single quote not analyze characters inside, so if you did: echo '$message', it just display $message.
Double quotes analyze characters inside, so if you did: echo "$message", it displays the content of $message variable.

Finwe is totally right.
Explanation: single quotes always take the string as it is without processing variables or metasigns like \t or \n.
just a hint in addition: try using only \n and not \r\n since there was a bug in some PHP-version that produced erros. even if this is not a problem at the moment, it might become one after an update (depending on your PHP-version).

Related

Sending email in PHP - Form

I have been trying to do tests to send an email through a form with php, but it does not work for me. The following message is returned: Error sending the email. That is, the value of the variable boolean $ sent is false.
I use Apache, XAMPP for Windows 10 with the Netbeans 8.2 development environment
Let's see if anyone can help me, thanks in advance.
This is the code that is giving faults:
<?php
$for = 'email#example.com';
$title = 'Sending email from PHP';
$message = '<html><head><title> Email with HTML </title></head>
<body><h1> Email with HTML </h1>
This is an email that is sent in HTML format
<hr>Sent by my program in PHP</body></html>';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers. = 'Content-type: text / html; charset = utf-8 '. "\r\n";
$headers. = 'From: Name1 Name 2 <info#example.com>';
$sent = mail ($for, $title, $message, $headers);
if ($sent)
echo 'Email sent correctly';
else
echo 'Error in sending the email';
?>
as far as I can see, you have some concatenation issues, I m not sure those actually exist in your code or are due to copy paste. Try following, take care of $header concatenation and semicolons ; at the end of statements.
<?php
$for = 'email#example.com';
$title = 'Sending email from PHP';
$message = '<html>'.
'<head> <title> Email with HTML </title> </head>'.
'<body> <h1> Email with HTML </h1>'.
'This is an email that is sent in HTML format.'
'<hr>'.
'Sent by my program in PHP'.
'</body>'.
'</html>';
$headers = 'MIME-Version: 1.0'. "\ r \ n";
$headers .= 'Content-type: text / html; charset = utf-8 '. "\ r \ n";
$headers .= 'From: Name1 Name 2 <info#example.com>';
$sent = mail ($for, $title, $message, $headers);
if ($sent)
echo 'Email sent correctly';
else
echo 'Error in sending the email';
?>

Getting text to show up in a php email as it did in a text area input [duplicate]

Can somebody please help me with this mail script.
I'm simply trying to send an html email and part of the message is from a user textarea, which puts in \r\n.
I seem to be unable to use nl2br or any other similar function. The code below isn't what I'm using but still produces the error.
The code:
$to = 'example#gmail.com';
$subject = 'Test Subject';
$message_var_1 = 'test1 \r\n test2 \r\n test3';
$message = nl2br("
<div>
<div>$message_var_1</div>
</div>
");
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion() . "\r\n";
mail($to, $subject, $message, $headers);
$message_var_1 = 'test1 \r\n test2 \r\n test3';
PHP parses \r and \n only within ", not within '. So nl2br won't apply here.
$message_var_1 = "test1 \r\n test2 \r\n test3";
$message = '<div>
<div>'.nl2br($message_var_1).'</div>
</div>';
This ought to work.
This string contains embedded newlines so you'll end up with a few unwanted <br/>s.
$message = nl2br("
<div>
<div>$message_var_1</div>
</div>
");
You can:
$message = "<div>" . nl2br($message_var_1) . "</div>";
Or, its much easier to use a <pre> tag:
$message = "<pre>$message_var_1</pre>";
An alternative is to use HEREDOC.
$message = <<<OUTPUT
<div> some text
<div> some more text </div>
</div>
OUTPUT;
$message = nl2br($message);
Manual link for heredoc here

Send HTML PHP Email with EOF

I would like to send the message body as HTML. Below I have included the relevant coding. I'm using the following for the message:
$message = '
<html>
<body bgcolor="#FFFFFF">
<b>Project Inquiry: $company</b><br /><br />
<b>Name:</b> $name<br />
<b>Company:</b> $company<br />
</body>
</html>
';
//end of message
I'm using the following headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
How can I fix this so it sends as HTML? I tried using EOF but I'm not sure why it's not working either. Any help or guidance is greatly appreciated. Thank you
I think it should work.
Use something like this:
$message = "<html><body bgcolor='#FFFFFF'>";
$message = "<b>Project Inquiry:</b>". $company."<br /><br />";
$message = "<b>Name:</b>". $name."<br />";
$message = "<b>Company:</b>". $company."<br />";
$message = "</body></html>";
Try this post, I posted to show how to send mail in both plain text + HTML version, so that it will even work well for most mail clients too and for browsers it will be HTML mails.
http://stackoverflow.com/questions/22070358/universal-send-email-html-or-plain-text-php/22070726#22070726
Sometimes, the server you are sending mails to, might not allow mails to be sent from local ips..

Sending HTML email from php with variables

I'm not very good at php so this should (hopefully) be easy for one of you.
I'm trying to send an HTML email from php which includes variables. I'm able to send a none html email with variables, or an HTML email without variables, but I can't seem to do both at the same time. Here's my code:
<?
$to = "me#something.co.uk";
$from = "m2#something.com";
$subject = "Hello! This is HTML email";
'
//begin of HTML message
$message = <<<EOF;
<html>
<p>Opt out of contact by phone<br />
'. $_POST["Opt_out_phone"] .'
</p>
</html> EOF;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
echo "Message has been sent....!"; ?>
I've never come across <<< EOF before and have no idea what it's doing.
You're using HEREDOC syntax and attempting to terminate the string and append the variable incorrectly and unnecessarily. To declare your string with the variable, do this:
$message = <<<EOF;
<html>
<p>Opt out of contact by phone<br />
{$_POST["Opt_out_phone"]}
</p>
</html> EOF;
Note that the PHP manual entry on heredoc syntax is with the rest of the string docs, here.

PHP line breaks don't seem to work

I have the following code that sends a message using the mail() function. Everything works fine except the line breaks at the end of each line don't seem to work. As you can see I am using " \r\n " hoping that this will give me a line break but it doesn't I have added <br> to get the break, but I would rather not use that in case someone doesn't have an HTML email client.
<?php
$to = 'user#example.com'; // Was a valid e-Mail address, see comment below
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$content = 'Name: '.$name."\r\n";
$content .= 'Email: '.$email."\r\n";
$content .= 'Subject: '.$subject."\r\n";
$content .= 'Message: '.$message."\r\n";
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
// Additional headers
$headers .= 'To: iVEC Help <help#ivec.com>'. "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
mail( $to, $subject, $content, $headers);
?>
<p> You sent it ... good work buddy </p>
<p> <?php '$name' ?> </p>
You're sending it as HTML - change the content type to text/plain and it should work.
The problem is that you say, in your headers, that the mail is an HTML email:
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
If you want to ensure compability with both HTML and text clients, consider using the Multipart content type. This can be achieved in many different ways, but one simple way is to use a library that can do this for you. For example, you can use PEAR:Mail_Mime
I'm not an expert in this area, but my guess would be that you're setting the content type to text/html, which implies html-rendering (which means line breaks are translated to a space). If you're not using any HTML-elements (which appear not to), try setting the content type to text/plain.
As mentioned before, either set it to text/plain or add a HTML break for line breaks:
$content = 'Name: '.$name."</br>\r\n";

Categories