wp_mail sending only plain text messages - html

I am sending some emails using wp_mail() function to send some HTML formatted emails. The problem is that the emails all look like basic plain text.
I set my headers to
$headers = array('Content-Type: text/html; charset=UTF-8');
In Windows, I use Fake SMTP server to test. The Content/Type is being recognized as text/html; charset=UTF-8
The problem is that all my emails look like basic plain text. No spacing from divs, no tables, no CSS modifications, nothing. It basically takes the text from the HTML that I wrote and puts it inline. I also tried sending the emails to GMAIL and no luck, it looks the same.
I ran out of options with testing this. What should I change?

Have you enabled HTML Headers in functions.php
add_filter( 'wp_mail', function( $params ) {
$params['headers'] = 'Content-type: text/html';
return $params;
} );

Related

Sending current HTML page in an email with mailto

I have a page (specifically, a Chrome developer extension) used for debugging client installations of some software. If clients need more help for a specific problem that they are observing, we want them to be able to email us with documentation of the problem.
Is there a way for a mailto to add (as an attachment or a frame within the email body) a file of the current html page the mailto is on? The page is dynamically generated locally, so it would be preferable if the page didn't have to be uploaded anywhere but the email.
Look at this w3schools example: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links_mailto_subject
(formatted excerpt, you will want the href="" to be all on one line)
<a href="
mailto:someone#example.com?cc=someoneelse#example.com
&bcc=andsomeoneelse#example.com&subject=Summer%20Party
&body=You%20are%20invited%20to%20a%20big%20summer%20party!" target="_top">
Send mail!
</a>
You can use javaScript to insert into the link the page as you need.
EDIT:
Just noticed that you might want to be careful with 'larger' pages, as there is a limit to how long a GET request (which this is) can be: maximum length of HTTP GET request?
tl:dr:
You might want to narrow the scope of what code is included if you use this method
Short answer: No.
The mailto in an a tag is only used to specify a link to an email, not the contents of said email. You would need to use some sort of server-side AJAX call. I would recommending using PHP's mail() function if you can.
Example
You would need to set the email header to be HTML compliant.
So, if you're using the PHP mail() function:
$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";
mail("target#something.com", "subject", "PLACE HTML HERE", $headers);
Then you could theoretically just pass off in the AJAX request all of the data from the page:
$.ajax({
url: "http://my.url.com/endpoint/",
method: "POST",
data: {
page: $("html").html()
}
});
Which you would then just embed somewhere in the email, or straight into the body. You could even add extra data for the GET and POST parameters present.
Note
While I can see this being used for some debugging, a lot of errors are caused via Javascript failing in some way or your PHP/server-side code failing. I'd recommend that whatever path you choose, including one I haven't covered, you should also include data from the console, POST, and GET variables if you have access to those (although be careful not to expose the POST and GET variables unnecessarily).
There are also a lot of tools like Chrome Remote Desktop that can help you view specific errors and problems that users are experiencing.
Alternately, to get around the mail() function, you could have embedded debugging Javascript which can dynamically send debugging information from their browser (via an AJAX request) to a server, that intercepts it for you to analyze and debug.
its not possible to include attachments using mailto:
one way to do this is to use mailto: to create a text message with two links:
the page url
the url of a png capture of the page.
there are chrome apis to capture the screen, and you can use your server or something like imgur to save the capture. probably better to use your own server that receives the images as imgur could be a privacy concern for your users.
It seems like a full answer is impossible, but I thought I'd share the route I took.
I have a button with id='save-log'. In the page where I add all the necessary events, I have
document.getElementById('save-log').addEventListener('click',
function(e){
chrome.runtime.sendMessage({
log: document.body.innerHTML
});
}
);
Then in a background page I have
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
chrome.tabs.create({
url: "save-log.html?" + message.log
});
}
);
And finally, in save-log.html I have the necessary styling information, with an empty body, and run the script:
document.write(decodeURIComponent(location.search.substring(1)));
Now there's a new tab with a full copy of the developer extension panel, and the user can save the html page and send it to whoever they want.

Open default mail app from within Qt with some html

How can I open the default compose mail window from the user's mail app from within a Qt app?
I found there is some class for mobile with Qtmobility, but I don't have access to this class as I'm working on a desktop app.
I also found people to use a URL sheme with a mailto in it. This isn't working for me because the html is stripped at some point, probably because of the url being too long and the html is not rendered in html but in plain text.
How can I precompose a mail in Qt and open the default mail app?
There is no built in way in Qt to send email with HTML formatting. The Mailto method will work for unformatted text, e.g.
QDesktopServices::openUrl(QUrl("mailto:?to=recipient#example.com&subject=The subject of an email&body=Here is some email body text", QUrl::TolerantMode));
But this cannot be used for html formatted text.
If you absolutely need HTML you will need to look at the options for your platform(s):
MAPI for Windows
AppleScript and Mail.app on OSX
Mail on Linux
Old topic but :
You could also try another way, as I did, using a web service.
I have a php web service that send email to a specific mail address, so I just send the message data to this web service, that will handle the rest for me.
This is to abstract yourself of using a desktop software that most of the time users don't have ( we all use gmail anyway, so you know ... ).
In php :
// sending mail to my#address.com
$headers ='From: sender#address.com'."\n";
$headers .="Reply-To: replyto#address.com"."\n";
$headers .='Content-Type: text/plain; charset="iso-8859-1"'."\n";
$headers .='Content-Transfer-Encoding: 8bit';
mail('my#address.com', '[TAG] mail subject', "some body text.", $headers);
Careful of security though !

How to send HTML content in email from net//smtp in ruby?

Pertinent code:
msg = "Subject: Reset password instructions\n\nHello " + #request_payload["email"] + "!\n\n" +
"A new account has been created for you at <a href=\"presentation-layer.dev\">presentation-layer.dev<a>." +
"Please go to that page and click \"forgot password\" to set your password."
smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start('domain', "email", 'password', :login) do
smtp.send_message(msg, 'sender', "recip")
end
The resulting email just has the raw text in it. How do I get the server to evaluate the HTML tags?
To do what you want, you should generate a MIME document. If you really want to do it right, create a multipart MIME document so you have both the TEXT and rich-text parts.
You can do it from Net::SMTP, but you have to add the necessary MIME header and part dividers to the document. See "Sending Email using Ruby - SMTP" for an example how.
It's easier to use the Mail gem, which supports both, especially if you're including multiple parts or adding attachments. From the documentation:
You can also create MIME emails. There are helper methods for making a multipart/alternate email for text/plain and text/html (the most common pair) and you can manually create any other type of MIME email.
And farther down in the document in "Writing and sending a multipart/alternative (html and text) email":
Mail makes some basic assumptions and makes doing the common thing as simple as possible.... (asking a lot from a mail library)
mail = Mail.deliver do
to 'nicolas#test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel#test.lindsaar.net.au>'
subject 'First multipart email sent with Mail'
text_part do
body 'This is plain text'
end
html_part do
content_type 'text/html; charset=UTF-8'
body '<h1>This is HTML</h1>'
end
end
Sounds like you need to set the 'Content-Type' header:
Content-Type: text/html;

How do I change to a different website using html or php logic?

I have spent WAY too much time searching for this. I know it must be a simple solution so I must be not thinking clearly. here is what I want:
I have some html code and php code in php file.
I perform some logic in php. Based upon this logic, I wish to send the user
to a different web page (url).
I tried the "Redirect", but all I get is an error message about Headers already being sent.
is there a simple solution in HTML or PHP? I also tried looking at the Javascript solution but that needs to be in the header, and this code is in the body.
Thanks for the help. I searched like mad and couldn't find this answer in any OPEN discussions.
John
Your problem is that the PHP code is embedded in your HTML code. When the server reads your HTML code, it begins to send that output to the output buffer. As such, any PHP code that wants to re-direct to a completely new page must come BEFORE any HTML code, or be in a completely separate file, as the buffers cannot be modified once they are already sent.
The easy answer: just make sure your PHP code that does the header() redirect function comes before any HTML code in your file, or put this code in a separate file.
Make sure that header is before any output
Correct
<?php
if ( some condition ){
header('Location: http://www.test.com/');
exit;
}
?>
<htmL>
Incorrect will give error
<htmL>
<?php
header('Location: http://www.test.com/');
?>
In PHP, headers need to be sent before any HTML is displayed. Probably the easiest way to do that is to submit a form:
<?php
// Since this is before the DOCTYPE, you can still send headers.
if (isset($_POST['foo'])) {
header('location: new-url.html');
}
?>
<!DOCTYPE html>
<html>
<!-- rest of the page goes here -->
Chances are you have HTML content before your PHP:
<span>This is HTML!</span>
<?php header("Location: http://www.example.com"); ?>
This won't work because you can't use the header function after having sent output. The HTML code counts as output so the example is the same as:
<?php
echo "<span>This is HTML!</span>"
header("Location: http://www.example.com"); // FAIL
?>
... here the output is very clearly before the execution of header, so it won't work.
It won't work because of the way HTTP responses are structured: headers have to be sent before the response body.
Under the hood, the output of a PHP script is a HTTP response:
HTTP/1.1 200 OK
Content-Type:text/html
<span>This is HTML!</span>
Usually the first line and the headers are added to the response implicitly and your PHP code only outputs the body, but when you use the header function you're adding headers, which have to come before the response body.
This means you have to call header before you output any of the response body:
<?php
header("Location: http://www.example.com");
echo "<span>This is HTML!</span>"
?>
... resulting in something like:
HTTP/1.1 303 See Other
Content-Type:text/html
Location: http://www.example.com
<span>This is HTML!</span>

Drupal webform html mail

I've made a contact form with Webform module. But it doesn't send HTML emails. I have installed HTML mail, Mail MIME modules. HTML mail module's send test works fine, but mail from Webform is always converted to plain form instead of HTML. I've tried to set email headers using this function:
function mytheme_webform_mail_headers($variables) {
$headers = array(
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')'
);
return $headers;
}
But it still doesn't work.
Edit: I've found that setting header works, so the mail is send as HTML, but the problem is, that content of email is converted to plain format (all HTML tags removed and "formatted" respectively)
it's an old post, but maybe still useful:
http://drupal.org/project/mimemail
this modul adds a checkbox to select, weather you want to send html-mails or not
Looks like this should be a built in feature (as it was in Drupal 6 version of the webform module) but there are some issues with the Drupal 7 version. The webform module code points to issue https://drupal.org/node/1043086. Keep an eye on that thread for an update.
Probably, You should use mailsystem module together with htmlmail module, to define the email processing spesially for Webform module.
http://drupal.org/project/mailsystem
http://drupal.org/project/htmlmail