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.">";
Related
I want to convert the link to the button in PHP mail.php file.
$message = sprintf($this->language->get('mail_message'), $store_name, $link);
I will appreciate if someone helps me to convert the link to the button.
To achieve this, you have to send html email instead of plain/text email. It is pretty simple, leave the images on the server and send the PHP + CSS to them...
$to = 'xyz#example.com';
$subject = 'Subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: abc#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p>Anchor Text</p>';
mail($to, $subject, $message, $headers);
$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
This question already has answers here:
PHP mail: All emails are received in the SPAM folder [closed]
(4 answers)
Closed 8 years ago.
this is the mail body header subject and message
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= "Content-Type: text/html" . PHP_EOL;
$headers .= "From: domin name" . PHP_EOL;
$subject = "1+1 Weekend Bonanza from Pizza Hut";
$message = "";
$message .= '<html>
message body
</html>';
*this is the mail function which i'm using.
mail($to, $subject, $message, $headers);*
My mail always go in the spam folder help me to send it in the inbox as it is very important to send them in the inbox pleae help me out.
Thanks for help in advance.
I'm unable to post the image as my reputation is low. in place of image i have written their is a image.
Email usually arrives to span because of bad headers. Try looking here.
<?php
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n"
?>
This additional headers for ur emails might help you
$subject = "1+1 Weekend Bonanza from Pizza Hut";
$header = "from: domin name \r"."<br >";
$header .= "Content-Type: text/html; charset=ISO-8859-1 \r"."<br >";
$header .= "Return-Path: <mail#domainname.com> \r"."<br >";
$header .= "X-Priority: 1 (Highest) \r"."<br >";
$header .= "X-MSMail-Priority: High \r"."<br >";
$header .= "Importance: High \r"."<br >";
$header .= "MIME-Version: 1.0 \r"."<br >";
$message = "<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=UTF-8' />
//Add more here
I've tried many header changes to no avail.
Could it be the email server? Or am I missing something crucial?
I would like to see more than the code rendered as text in the resultant email.
Thanks!
<?php session_start(); ?>
<?php
$body = '';
$body .= '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
</head>
<body>
<h1>Here is a Copy of Your Order</h1>';
$body .= '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
$body .= "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
}
$body .= '</table>';
$body .= "<p>Grand total: \$$total</p>";
$body .='</body></html>';
}
?>
<?php
echo $body;
$to = 'xxxx#gmail.com';
$subject = 'Fill This Order';
$headers = 'From: xxxx#gmail.com' . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
mail($to, $subject, $message, $body, $headers);
?>
here is a tutorial about how to send a mail with html code in it: Sending Nice HTML Email with PHP
work around it, what they got works.
as you can easly see there, the headers sent is:
$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";
they diffrence you can see is mostly: \r\n in every end of line.
this could be your problem because without it it can be something like:
$headers .= 'MIME-Version: 1.0' . PHP_EOL . 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
and in headers... this could be: 'MIME-Version: 1.0Content-type: text/html; charset=iso-8859-1
and not work..
i think you should take a look in the link i added and modify your code by it.
EDIT:
i think i found it! you sent mail($to, $subject, $message, $body, $headers);
while PHP mail function uses:
mail(to,subject,message,headers,parameters)
you can see here how to use the mail function: The PHP mail() Function
the thing i think heppend is you sent $message which does not exsits and the headers are not sent as they should.
Have you tried swapping out PHP_EOL for "\r\n"?
My script send the email but it doesn't render the html tags. I'm not sure why not.
$email = $row['email'];
$mail_body = '<html>
<body>
<p>hello,'.$name.'</p>
<p>this is a testing email </p>
<hr />
<p>by server</p>
</body>
</html>';
$subject = "Better website";
$to = "$email";
$headers = "From: mailscript#hotmail.com\r\n";
$headers .= "Content-Type: text/html\r\n";
$mail_result = mail($to, $subject, $mail_body, $headers);
Try setting the mime type as well as shown in the manual for mail()
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
as Jrod already answered you need to add the header
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
but I found that this one is screwing mine up as of late and had to comment it out:
$headers = 'MIME-Version: 1.0' . "\r\n";