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

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;

Related

wp_mail sending only plain text messages

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

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.

When sending an email with meteorjs, the base64 inline image is not displayed correctly

Current situation:
(Client side) The template:
<template name="feedback">
<h1>The Image</h1>
<img src="{{image}}" alt=""/>
</template>
(Client side) Calling the mail function:
var dataContext={
image: canvas.toDataURL('image/png')
};
var html=Blaze.toHTMLWithData(Template.feedback, dataContext);
Meteor.call('feedback', html);
(Server side):
Email.send({
to: 'xxx',
from: 'xxx',
subject: 'xxx',
html: html
});
Expected result: A nice email with an embedded image.
Actual result: I get a mail partially html, partially 'raw' text. I have no clue to why.
This is a part of what I see in the raw email:
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<h1>The Image</h1>
=20=20=20=20<img =
src=3D"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAALKCAYAAADTWUxrAA
Any ideas?
Gmail, as well as many other clients, do not allow you to use base64 as a source in an img tag. There are ways around this though:
First and easiest might be to just keep the image on your server and put a url into the image source tag. This has the added benefit of being able to handle some tracking (some additional development required).
Second would be to use a third party mail system and send them the image and the HTML and set it up this way. This might be good for a number of reasons but it doesn't really answer your question.
Finally you can do this in an email template like you have by adding a multipart multitype boundary solution very similar to this answer:
base64 encoded images in email signatures

What is the syntax for using cid:attachmentID in e-mail for image file used in css

I am working on a script that e-mails some formatted html and images to recipients. Using MIME::Lite, I figured out a way to send the css file and the image file it uses as attachments. The image comes out at the end of the mail message - as an attachment. The following line appears to work:
<link href="cid:style.css" rel="stylesheet" type="text/css" />
My question is what should be the syntax for the following lines (in the file style.css)? Following does not work.
body {
background-image:url("cid:bgLine.png");
background-repeat:repeat-x;
}
Furthermore, how can I stop the mail client from showing the image by itself? Script I am using follows:
my $msg = MIME::Lite->new( From =>"from\#company\.com",
To => "to\#company\.com",,
Subject =>"Action Required",
Disposition =>'inline',
Type =>'multipart/related');
$msg->attach(Type => 'text/html', Data => qq{#htFileContents});
$msg->attach(Type => 'text/html', Id => $cssFileName, Data => qq{#cssFileContents});
$msg->attach(Type => 'image/png', Id => $imageFile, Path => $imageFile);
$msg->send("sendmail","/usr/sbin/sendmail -t");
Having the mail client access an URL for the css or the image file from an http server is not an option. The e-mail needs to be self-contained. TIA for an example showing the syntax.
This won't really work. Even if by some miracle you were able to get your attachments to see and talk to each other, Outlook won't support background images, Gmail will strip out your linked css file and any embedded css, and hotmail will not support css backgrounds.
Sounds like you need to host an HTML email somewhere that accepts dynamic parts, and trigger the send from within your application, passing it the dynamic parts to fill in before sending. Check out a tool like Lyris Listmanager or something.
There is no connection between the title and the posted question above.
Anyway, in order to send emails, showing safely images, that are attached in the email, it just not enough to refer the images with some cid: URLs.
Attaching the images to the email has the following specifics:
Replace the external URLs to images with the URLs of an attachments in the email.
The email to generate is not just multipart/mixed what is construed by default of JavaMail, but MIME multipart/related type, in order to allow the email client to use the images.
The attachments for the images must be have
Content-Disposition: inline; filename=... header, in order they not to be shown as attached files, but to allow the email client to use them.
ContentID:unique-name#domain header, where the unique-name and domain have to be replaced with actual values and keep < and >!
Example: ContentId: logo.png#paysafe.com
The references in the HTML part of the email message (the message body) happens through cid:content-id URLs, where content-id is the the value of the ContentId header of the attachment holding the image
Example:
References:
https://javaee.github.io/javamail/docs/api/ for the mail protocol-specific properties.
https://www.rfc-editor.org/rfc/rfc2392 - defines the CID: URL schema and the use of addr-spec
https://www.rfc-editor.org/rfc/rfc822 - defines addr-spec in the form: local-part "#" domain
https://www.rfc-editor.org/rfc/rfc1806 - defines Content-Disposition header
https://www.rfc-editor.org/rfc/rfc2387 - The MIME Multipart/Related Content-type - vital for image inlining

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