I´m sending emails through a Rails web app using AmazonSes service. I´m sending different types of emails and everything is working fine, but in the next scenario:
The user can write a text like a message with line breaks in a textarea field within a form. This message is stored in our MySQL dababase and sent by email to another user. I have problems with the line breaks, because depending on if I open the email in Gmail webmail or Mac Mail, the break lines are respected or not. I have read a lot of post with similar problem and tried but unfortunately I couldn´t fix it.
As mentioned, I´m using Rails Mailer to send the emails through AmazonSES services.
Input textarea with message:
This is what I store from the textarea field in database: "messagePreOffer"=>"This is paragraph 2.\n\nThis is paragraph 2.\n\nThis is paragraph 3."
Before rendering the mail template the messagePreOffer log shows:
offer.message_pre_offer=This is paragraph 1.
This is paragraph 2.
This is paragraph 3.
Simplified mail template layout:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- NAME: 1:2 COLUMN -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p><%= #offer.message_pre_offer %></p>
</body>
</html>
We send the email with mailer (I think this is not relevant):
mail(from: APP_CONFIG['EMAIL_FROM'], to: offer.email, bcc: APP_CONFIG['EMAIL_FROM'], subject: t('new_offer_user_subject', departure: #offer.enquire.departure))
Mailer delivery configuration:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "email-smtp.eu-west-1.amazonaws.com",
:port => 587,
...
:authentication => :login,
:enable_starttls_auto => true
}
TEST 1:
This is the log that I got after running and sending the email:
Rendering email_service/send_new_offer_user_notification.html.erb within layouts/email_service
Rendered email_service/send_new_offer_user_notification.html.erb within layouts/email_service (2.0ms)
EmailService#send_new_offer_user_notification: processed outbound mail in 75.5ms
Sent mail to *****#gmail.com (1117.1ms)
Date: Wed, 16 Dec 2020 17:56:23 +0100
From: *******
To: *****#gmail.com
Message-ID: <5fda3c373232d_42433fc7916a717c9102b#MacBook-Pro-****.local.mail>
Subject: Whatever
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5fda3c3731200_42433fc7916a717c909a4";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4
Content-Type: text/plain;
**charset=UTF-8
Content-Transfer-Encoding: 7bit**
This is paragraph 1.
This is paragraph 2.
This is paragraph 3.
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4
Content-Type: text/html;
**charset=UTF-8
Content-Transfer-Encoding: 7bit**
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- NAME: 1:2 COLUMN -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<p>This is paragraph 1.
This is paragraph 2.
This is paragraph 3.</p>
</body>
</html>
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4--
With this initial configuration, I get the next results:
Result 1: Same result in Gmail and Mac Mail.
I understand this is right, because I didn´t add any format.
TEST 2:
While making tests to write this post I went one step further. My second test just added the next css, to keep line breaks:
<p style="white-space: pre-line;"><%= #offer.message_pre_offer %></p>
After running the test, I got very surprises that it worked perfectly in both Gmail webmail and Mac mail. Find screen here:
TEST 3:
The lines used in the previous test where just for testing purposes. As the tests worked, I tried to use the real text template in spanish. Surprisingly the test didn´t work.
This is the variable log:
"messagePreOffer"=>"Este es el párrafo 1.\n\nEste es el párrafo 2.\n\nEste es el párrafo 3."
This is the log that I got after running and sending the email:
Sent mail to ***#gmail.com (1130.8ms)
Date: Wed, 16 Dec 2020 18:53:23 +0100
From: *****
To: *****
Message-ID: <5fda499338a0f_42433fc7916a717c946d1#MacBook-Pro-de-******.local.mail>
Subject: Whatever
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5fda49933789a_42433fc7916a717c945df";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5fda49933789a_42433fc7916a717c945df
Content-Type: text/plain;
charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**
Este es el p=C3=A1rrafo 1.=0D
=0D
Este es el p=C3=A1rrafo 2.=0D
=0D
Este es el p=C3=A1rrafo 3.=
----==_mimepart_5fda49933789a_42433fc7916a717c945df
Content-Type: text/html;
charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.o=
rg/TR/xhtml1/DTD/xhtml1-strict.dtd">=0D
<html xmlns=3D"http://www.w3.org/1999/xhtml" xmlns=3D"http://www.w3.org/1=
999/xhtml">=0D
<head>=0D
<!-- NAME: 1:2 COLUMN -->=0D
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8" =
/>=0D
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale=3D=
1.0" />=0D
</head>=0D
<body>=0D
<p style=3D"white-space: pre-line;">Este es el p=C3=A1rrafo 1.=0D
=0D
Este es el p=C3=A1rrafo 2.=0D
=0D
Este es el p=C3=A1rrafo 3.</p>=0D
</body>=0D
</html>=0D
----==_mimepart_5fda49933789a_42433fc7916a717c945df--
Then in Mail Mac the format is still ok:
However, in Gmail webmail the break lines are broken, with many lines and much more space:
After some tests with the content text, I have found out that the problem starts with the accents. If you take a deep look into the logs, in the first test the charset is:
charset=UTF-8
Content-Transfer-Encoding: 7bit
However, as soon as I include an accent in my text, the content transfer encoding automatically changes to:
charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**
This is the root cause why the break lines are broken. Actually, If I just remove the accent, everything works in Gmail again:
Now, I found the root cause of the problem, but I´m stuck figuring out which is the right solution.
After much more investigation I found this other interesting post: Why isn't Gmail using quoted-printable encoding?
that pointed me that Gmail is not working with quoted-printable. So, I have fixed the problem by forcing the mailer to use 7-bit transfer enconding, with next line in my mailer:
default 'Content-Transfer-Encoding' => '7bit'
I was facing the same issue even with default content_transfer_encoding: 7bit
Rails was still encoding my mail with quoted-printable when ever replying email with large html message.
After spending a day, found this solution working:
mail(
to: ...,
subject: ...,
content_type: 'text/html',
) do |format|
format.html(content_transfer_encoding: 'base64') { Base64.encode64(#mail_box.message) }
end
#mail_box.message is the html contents supplied from a rich text editor. Hope this helps someone.
Related
How can I send a message using Microsoft Graph that would have both, text AND html bodies?
For those finding this message sometime later, pulling your hair out trying to understand why Microsoft didn't make this clear in the documentation, see this post: Can Office 365 REST API send an email with both plain text and HTML body?
I've confirmed this on my end - once the graph API request is made, the Exchange server does, in fact, strip the HTML characters, creates a multipart message (with boundaries) and delivers in the appropriate format.
In my case, I only needed to set the following:
body.contentType = BodyType.HTML;
body.content = myCustomEmailObject.getHtml();
Here's an example of what the raw response (email source) looks like in gmail:
--_000_CO6PR14MB42581EA88B5BE054EFE62113CF449CO6PR14MB4258namp_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
This is the content
And this is a new line...
--_000_CO6PR14MB42581EA88B5BE054EFE62113CF449CO6PR14MB4258namp_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
</head>
<body>
<b>This is the content</b><br>
And this is a new line...
</body>
</html>
--_000_CO6PR14MB42581EA88B5BE054EFE62113CF449CO6PR14MB4258namp_--
We are trying to send html email via MVC postal nuget package. The problem is some clients are seeing raw html tags in the email. Sending the same email to gmail and a few other providers causes no issues. Any ideas?
We are following the simple conventions as per the docs.
Email.cshtml
#model InviteViewModel
To: #Model.Email
From: invites#domain.com
Subject: #Model.Subject
Views: Html
Email.Html.cshtml
#model InviteViewModel
Content-Type: text/html; charset=utf-8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
bla blah......
</html>
The Content-Type: text/html; charset=utf-8 of the *.Html.cshtml file must be at the topmost line of the file.
Email.Html.cshtml
Content-Type: text/html; charset=utf-8
#model InviteViewModel
<html>
bla blah......
</html>
I am currently working on Classic ASP for one of my project. For non English languages I am getting boxes instead of special characters. I am rendering using UTF-8 but sometimes the characters goes to boxes. It comes back normal when I click refresh sometimes.
I followed all the steps below but i still get this problem
XML:
<xml version="1.0" encoding="UTF-8">
HTML:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
content-type: text/html; charset=utf-8
Am I missing anything here? Thanks.
Add this to your page:
Response.CodePage = 65001
Response.CharSet = "utf-8"
It should display all correctly now.
Hope that helps
It's amazing BUT nobody gives a complete answer on what to do this PROPERLY... I hope this help somebody like me, because it was so hard to find the whole picture...
---------------- PREVIOUS CONSIDERATIONS --------------
FIRST, make sure IIS IS NOT replacing the Code Page... Go to IIS, click the Website, open ASP module, on Behavior it should be >> Code Page = 0
SECOND, The file itself should be checked, YES! the file... open your file explorer on windows (my computer), go to the folder where the files of your website are, take for example "default.asp", right click >> open with >> notepad THEN click on File >> Save As... IN THE DIALOG at the bottom says "Encoding", make sure it has UTF-8, otherwise you will have to add the
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> in every page (including server side includes), which is not correct.
---------------- CORRECT STRUCTURE OF THE PAGE --------------
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><%
Response.AddHeader "Content-Type", "text/html;charset=utf-8"
%><!-- #include virtual="/conexion.asp" -->
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
</head>
<body>
áéíóú
</body>
</html>
It should work fine now with QueryStrings, Database and regular HTML... uffff
I have a simple Google maps web application I'm working on. I have purchased a domain name for the application (http://www.jcunav.com), which during my testing, is designed to simply forward to a page which is hosted on my another domain name of mine (http://www.codeemporium.com/experiments/map5.html). Testing on my Android Nexus S, I am noticing strange behaviour however - if I visit http://www.codeemporium.com/experiments/map5.html directly, then the app displays as intended - the map is the size I want and clicking the "About" link brings up a dialog the size I want. If I visit http://www.jcunav.com however (which, keep in mind, simply forwards to http://www.codeemporium.com/experiments/map5.html), the map displays at what looks like a more zoomed out level, and pressing the "About" link at the bottom of the page shows a dialog box which also appears more zoomed out. My question is, what could be causing this to occur, given that all http://www.jcunav.com is doing is forwarding to http://www.codeemporium.com/experiments/map5.html...
Are you certain you aren't frame-forwarding? I examined the page in firebug for each of the links you provided and it appears to me that you are frame-forwarding the URL. This results in your target page being 'wrapped' in a frame when presented to the end user. Odds are high that this is why it is not working for you.
Here is a somewhat lossy version of what is in the forwarded page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>JCU Nav</title>
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</head>
<frameset frameborder="0" framespacing="0" border="0" rows="100%,*">
<frame name="MYTOPFRAME" src="http://www.codeemporium.com/experiments/map5.html" noresize>
-- snip --
</frameset>
</html>
Notice the frame tag:
<frameset frameborder="0" framespacing="0" border="0" rows="100%,*">
<frame name="MYTOPFRAME" src="http://www.codeemporium.com/experiments/map5.html" noresize>
This is what happens when you frame forward.
Since your sizing relies on additions to the HTML tag:
<html class="ui-mobile landscape min-width-320px min-width-480px min-width-768px min-width-1024px">
They do not work in the frame-forwarded version because they are nested inside the frame and not on the root page.
That's because http://www.jcunav.com is not forwarding to http://www.codeemporium.com/experiments/map5.html, it is loading it into a frame:
C:\Documents and Settings\blah>wget -S -O - http://www.jcunav.com/
--01:05:21-- http://www.jcunav.com/
=> `-'
Resolving www.jcunav.com... 66.150.161.141, 69.25.27.173, 63.251.171.80, ...
Connecting to www.jcunav.com|66.150.161.141|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Sun, 01 May 2011 05:01:11 GMT
Server: Apache/2.0.49 (Unix) PHP/4.3.9
X-Powered-By: PHP/4.3.9
Content-Length: 823
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Length: 823 [text/html]
0% [ ] 0 --.--K/s <
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>JCU Nav</title>
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</head>
<frameset frameborder="0" framespacing="0" border="0" rows="100%,*">
<frame name="MYTOPFRAME" src="http://www.codeemporium.com/experiments/map5.html" noresize>
<noframes>
<body>
<h1>JCU Nav</h1>
<br>
<br>
<br>
Click here to enter <a href="http://www.codeemporium.com/experiments/map5.html">http://www.codeemporium.com/e
xperiments/map5.html</a>
<hr>
| Domain Name Registration and Domain Name Forwarding by <a href="http://www.mydomain.com">mydomain.com - Register your
domain name</a>
</body>
</noframes>
</frameset>
</html>
You'll need to actually change that frame set to do the right thing.
I need to be able to send the content type "text/xml" to Firefox and Safari, to allow them to render inline SVG in one of my pages.
This works, as long as the content type is "text/xml".
However, when IE hits the page, if the content type is not "text/html" it tries to render the XML document tree, rather than the XHTML content of the page.
What is the "right way" in ASP.NET MVC to set the HTTP Content-Type of ALL of my views?
Keep in mind that I am going to be rendering the views as ViewResults.
Ok, to clear any confusion up:
User Agent Content-Type Desired
-----------------------------------
IE 5.5 text/html
IE 6 text/html
IE 7 text/html
IE 8 text/html
Firefox text/xml
Safari text/xml
Chrome text/xml
And so on.
All of the browsers listed support SVG inline in some way or another. I have a consistent delivery format, save the content type.
You could look at the properties in Request.Browser and sniff out IE that way, and return the proper view that way, though that is prone to issues. This isn't optimal because IE might support it in the future.
public ActionResult MyAction() {
if (this.Request.Browser.Browser == "IE") {
return View("NonSVG");
} else {
return View("SVG");
}
}
Something worth looking into a little more might be this page on Codeplex. They define a property on Browser called AcceptsImageSVG, but it looks like it's geared towards mobile browsers, don't know if it could be used in your situation.
According to W3, you should be using application/xhtml+xml rather than text/xml to signify XHTML:
http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020801/#text-xml
The above article also notes that text/html should not be used for XHTML content.
You can determine the browser type by using the Request.Browser object
See this example http://msdn.microsoft.com/en-us/library/system.web.configuration.httpcapabilitiesbase.type%28VS.80%29.aspx
So you could do something like:
if( Request.Browser.Type.ToUpper().Contains("IE") )
{
// Return IE View
}
else
{
// Return the other view
}
Or, if you use this in lots of places you could create a ViewResult factory that returns the proper view result based on the browser type.
Kind of hacky, but... What if the SVG was in a frame, the frame advertised content-type=text/xml, while the containing page advertsized the more proper application/xhtml+xml. This divides the problem into two (possibly) more tractable ones.
I'm not sure whether this will work, but you could try using conditional comments:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<!--[if lte IE 8]>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<![endif]-->
<!--[if gt IE 8]>-->
<meta http-equiv="Content-Type" content="text/xml; charset=UTF-8" />
<!--<![endif]-->
<title>Test page</title>
</head>
<body>
<p>Test page</p>
</body>
</html>
Or, better yet, use HTML5:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Test page</title>
</head>
<body>
<p>Test page</p>
</body>
</html>
You don't even need to specify the content type.