CSS won't work when sending HTML email with wp_mail() - php

I am trying to send a HTML mail with the Wordpress function wp_mail() like this:
add_filter( 'wp_mail_content_type', function( $content_type ) { return 'text/html'; });
$headers = 'From: '. $current_user->user_firstname . ' ' . $current_user->user_lastname .' <'. $current_user->user_email .'>' . "\r\n";
wp_mail( $_POST['email'], $_POST['subject'], $_POST['mail_text'], $headers);
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
All HTML tags like <strong>, <li>, <p> etc. come with HTML format. But when I try to change the color using HTML or CSS like <li style="color:red">Word</li> or <li color="red">Word</li> it won't work and appears as blank text.
What am I doing wrong?

The text/message you send is in $_POST['mail_text'] coming from a textarea, right?
Did you echo out the content of $_POST['mail_text']? Is it ok before sending?
Try single quotes:
<li style='color:#f90'>Hello World</li>
and check if somewhere a script calls strip_tags to remove html tags.

$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\n";
$headers .= "X-Priority: 1 (Higuest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$headers .= "From: Approprice <".$mailfrom.">" . "\n";
$headers .= "Return-Path: Approprice <".$mailfrom.">" . "\n";
$headers .= "Reply-To: Approprice <".$mailfrom.">";
user this headers in email for sending html

Related

Mail is not in proper format in yahoo

when user register or clicks on forgot password mail is send. it looks fine in gmail and other accounts but it is shown in html format in yahoo.
code is
$embody="<p>Dear ".$data['name']." </p>
<p> Thank you for registering with JCA Associates. Please log into your account to complete your candidate profile and upload your CV </p><br/>
<p> JCA Associates</p>
<p> <img src='".$_SERVER['HTTP_HOST']."/themes/images/logo.png' width='100' height='60'></p>";
//} else {
// $embody="<p>Dear ".$data['name']." </p><p> Thank you for registration with us!</p><p> Best regards,<br/> JCA Team</p>";
//}
$message = '<html dir="ltr" lang="en">' . "\n";
$message .= ' <head>' . "\n";
$message .= ' <title>Welcome to JCA Associates</title>' . "\n";
$message .= ' <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . "\n";
$message .= ' </head>' . "\n";
$message .= ' <body><p> ' . html_entity_decode($embody, ENT_QUOTES, 'UTF-8') . '</body>' . "\n";
$message .= '</html>' . "\n";
it is shown like this in yahoo.
http://screencast.com/t/wDWixSBI
I'm guessing you haven't added the HTML headers to the function that triggers the email call. If you're using mail, add these headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
Where $subject, $to, and $headers have the appropriate values.
Try to adding following to your email code and it will work like a charm.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8 \n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP/"."MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html\n";
mail("$sendto","$subject","$embody","$headers","$from");

HTML not being rendered in email

$message = 'What are the other benefits to saying "nah" that you can think of? - ' . $_POST['other-benefits'] . '<br>' .
'What are you pledging to do? - ' . $_POST['what-pledge'] . '</p>';
I am trying to do a break after each line in php but with embedded HTML. When I send an email(function not included) it doesn't render with breaks. It literally just types out the as a string. Is there a way to correct this?
Given the lack of information given about the problem, I can only assume that the issue is that the headers of the email have not been set to send HTML emails.
You'll have to do something along the lines of:
// Setup email content.
$to = "example#email.com";
$subject = "Example HTML email";
$message = 'What are the other benefits to saying "nah" that you can think of? - ' . $_POST['other-benefits'] . '<br>' .
'What are you pledging to do? - ' . $_POST['what-pledge'] . '</p>';
// Setup headers.
$headers = "From: from#email.com\r\n";
$headers .= "Reply-To: replyto#email.com\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
// This next one is where the magic happens.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Send!
mail($to, $subject, $message, $headers);
Refer to: https://css-tricks.com/sending-nice-html-email-with-php/
In order to send an email containing html, pay special attention to the following headers:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Example Code:
$otherbenefits = $_POST['other-benefits'];
$whatpledge = $_POST['what-pledge'];
$email = <<< LOL
<html>
<body>
<p> What are the other benefits to saying "nah" that you can think of? - $otherbenefits <br> What are you pledging to do? - $whatpledge </p>
</body>
</html>
LOL;
$emailaddress = "personsemail#website.com";
$subject = "Test Email";
$headers = "From: noreply#server.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($emailaddress, $subject, $email, $headers);
For more information, please visit PHP.net regarding the mail() function and how to send email as HTML.
http://php.net/manual/en/function.mail.php

PHP mail displaying content type header inside the body

I'm making a form, when it submits it sends you a HTML email with a confirmation that you submitted it correctly.
When I receive the email; the content type header is inside the body of the email and the HTML code is displaying as raw text.
Screenshot:
Here is my code:
$to = 'myemail#gmail.com';
$subject = "HTML email";
$body = "<html><body><h1>Hello world!</h1></body></html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <webmaster#website.com/>' . "\r\n";
mail($to, $subject, $body, $headers);
Could anyone please tell me what exactly is going wrong?
Thank you
Change your below three lines
From
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <webmaster#website.com/>' . "\r\n";
To
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type:text/html;charset=iso-8859-1" . PHP_EOL;
$headers .= 'From: <webmaster#website.com/>' . PHP_EOL;
I think after that this will work properly

wp_mail line breaks in textarea for html email

I am having an issue using the wp_mail function, and hoping someone can help me.
I need to insert line breaks in the email that user added to the text area 'message.'
Can anyone please help?
I currently have the follow code sending an email from a contact form:
<?php
if( !isset($_REQUEST) ) return;
require('../../../../wp-load.php');
function wpse27856_set_content_type(){
return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$msg = $_REQUEST['message'];
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$message = '
<html>
<body>
Someone has made an enquiry on the online contact form:
<br /><br />
<b>Contact Details:</b><br />
'.$name.'<br />
'.$phone.'<br />
'.$email.'<br /><br />
<b>Message:</b><br />
'.$msg.'<br />
<br /><br />
</body>
</html>
';
wp_mail('email#email.co.uk', 'Contact form Message' , $message, $headers);
?>
By default, wp_mail() sends messages as plain text, so the HTML is not parsed by email clients.
Include the following headers with your email:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Since your email is in HTML, it's always good practice to make it valid with the proper HTML boilerplate (HTML, HEAD, BODY...)
Alternatively, you can replace your tags by carriage returns (\r\n), although you'll still need to get rid of tags.
This is already covered by the PHP documentation for mail(), around which wp_mail() wraps.
http://php.net/manual/en/function.mail.php
You can add header to your mail if your textarea contains html
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$msg = nl2br($_REQUEST['message']);
use this header for send html in email
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\n";
$headers .= "X-Priority: 1 (Higuest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$headers .= "From: Approprice <".$mailfrom.">" . "\n";
$headers .= "Return-Path: Approprice <".$mailfrom.">" . "\n";
$headers .= "Reply-To: Approprice <".$mailfrom.">";

mail() sends the message as .txt file

I'm pretty new to PHP so don't mind the #... was a quick fix for opening the script without errors
I'm currently working on a mail script and its working pretty well.
Sadly it sends the text the user enters as a .txt file instead of plain text
Basically the form is stored in messenger_mailsend.php and the content of the Textarea gets to messenger_mailsend_controller.php by using $_POST[].
#$inhalt = $_POST['inhalt'];
$message = strip_tags($inhalt);
if(isset($_POST['submit'])){
#mail($mailto, $subject, 'why not zoidberg?', $headers);
echo 'The mail was sent!';
}
'why not zoidberg' is the test text i used to check if theres sth wrong with the var, actually it says mail($mailto, $subject, $message, $headers);
is the code I use to get the typed message from the form, remove the tags and store it as $message.
If I do a var_dump it says 'I am content' (what was typed in the form)..
when the $message var is replaced by some plain text it still gets sent as TEXT.txt
The code i use for the $headers is:
$headers .= 'From:' . $usrmail . "\n";
$headers .= 'Reply-To:' . $reply . "\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\n";
$headers .= 'X-Sender-IP:' . $_SERVER['REMOTE_ADDR'] . "\n";
$headers .= 'Content-type: text/html; charset=ISO-8859-1\r\n';
$headers .= 'Cc: ' . $cc . "\n";
$headers .= 'Bcc: ' . $bcc . "\n";
-- Solution: By setting the content-type to text/plain it works, thanks anyone! --
Well how can you expect the mail to be sent as html when you are stripping all the html tags before sending....
However, if you want to send the html mail, you have to define the same in the headers before sending. See the snippet:
$to = 'bob#example.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
#$inhalt = $_POST['inhalt'];
$message = strip_tags($inhalt);
And then you send the mail like this:
mail($to, $subject, $message, $headers);

Categories