HTML not being rendered in email - php

$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

Related

Want to put button instead of link in mail

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

PHP/HTML Code Renders as Text in Email: Any Insight?

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"?

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

Send HTML in email via PHP

How can I send an HTML-formatted email with pictures using PHP?
I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?
The main problem is to attach files. How can I do that?
It is pretty simple. Leave the images on the server and send the PHP + CSS to them...
$to = 'bob#example.com';
$subject = 'Website Change Request';
$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=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the information from... (link)
You will need security though...
You need to code your HTML content using the absolute path for images. By absolute path, I mean you have to upload the images to a server and in the src attribute of images you have to give the direct path, like this <img src="http://yourdomain.com/images/example.jpg">.
Below is the PHP code for your reference: It’s taken from mail:
<?php
// Multiple recipients
$to = 'aidan#example.com' . ', '; // Note the comma
$to .= 'wez#example.com';
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<p>Here are the birthdays upcoming in August!</p>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I have this code and it will run perfectly for my site:
public function forgotpassword($pass, $name, $to)
{
$body = "<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/>support#pms.com</td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body, $to, $subject);
}
Mail function
function sendmail($body, $to, $subject)
{
//require_once 'init.php';
$from = 'testing#gmail.com';
$headersfrom = '';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: ' . $from . ' ' . "\r\n";
mail($to, $subject, $body, $headersfrom);
}
The image URL function is used for if you want to change the image. You have to it change in only one function. I have many mail functions, like for forgot password or create user. Therefore I am using the image URL function. You can directly set the path.
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
Sending an HTML email is not much different from sending normal emails using PHP. What is necessary to add is the content type along the header parameter of the PHP mail() function. Here is an example.
<?php
$to = "toEmail#domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
$headers .= 'From: name' . "\r\n";
mail($to, $subject, $message, $headers);
?>
You can also check here for more detailed explanations by W3Schools.
You can easily send the email with HTML content via PHP. Use the following script.
<?php
$to = 'user#example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: CodexWorld<info#codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome#example.com' . "\r\n";
$headers .= 'Bcc: welcome2#example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Source code and live demo can be found from here - Send Beautiful HTML Email using PHP
The simplest way is probably to just use Zend Framework or any of the other frameworks like CakePHP or Symfony.
You can do it with the standard mail function too, but you'll need a bit more knowledge on how to attach pictures.
Alternatively, just host the images on a server instead of attaching them. Sending HTML mail is documented in the mail() function documentation.
Use PHPMailer.
To send HTML mail, you have to set $mail->isHTML() only, and you can set your body with HTML tags.
Here is a well written tutorial:
How to send mail using PHP
The trick is to know the content id of the image MIME part when building the HTML body part.
It boils down to making the img tag—<img src="cid:entercontentidhere" />
Kronolith.php
Look at the function buildMimeMessage for a working example.

Categories