I have this html message that I would like to send using MIME format through a rest api call ( using microsoft send email api ).
@message = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><div dir=\"ltr\">Hello <div><br></div><div>This is the first email<br clear=\"all\"><div><div dir=\"ltr\" class=\"gmail_signature\" data-smartmail=\"gmail_signature\"><div dir=\"ltr\"><span><p style=\"color:rgb(34,34,34);line-height:1.2;margin-top:0pt;margin-bottom:0pt\"><br></p><div style=\"color:rgb(34,34,34)\"><br></div><div style=\"color:rgb(34,34,34)\"><font face=\"verdana, sans-serif\">Regards</font></div><p style=\"color:rgb(34,34,34);line-height:1.2;margin-top:0pt;margin-bottom:0pt\">--</p><div style=\"color:rgb(34,34,34)\"><div dir=\"ltr\"><div dir=\"ltr\"><p style=\"line-height:1.2;margin-top:0pt;margin-bottom:0pt\"><font face=\"verdana, sans-serif\"><span style=\"font-size:10pt;color:rgb(11,83,148);vertical-align:baseline;white-space:pre-wrap\">Md. Imam Hossain</span><span style=\"font-size:10pt;color:rgb(11,83,148);vertical-align:baseline;white-space:pre-wrap\">, B.Sc.(Engg.)</span><br></font></p><p style=\"line-height:1.656;margin-top:0pt;margin-bottom:0pt\"><font color=\"#999999\" face=\"verdana, sans-serif\"><span style=\"font-size:13.3333px;white-space:pre-wrap\">Software Engineer</span></font></p><font face=\"verdana, sans-serif\"><span style=\"font-size:10pt;color:rgb(153,153,153);vertical-align:baseline;white-space:pre-wrap\">Mobile: </span><span style=\"font-size:10pt;color:rgb(11,83,148);vertical-align:baseline;white-space:pre-wrap\">+880168</span></font></div><div dir=\"ltr\"><span style=\"color:rgb(11,83,148);vertical-align:baseline;white-space:pre-wrap\"><a href=\"https://www.linkedin.com/in/imamrb/\" style=\"color:rgb(17,85,204);display:inline\" target=\"_blank\"><font style=\"display:inline\" face=\"verdana, sans-serif\"><font color=\"#0b5394\">Linked</font><b><font color=\"#ffffff\" style=\"background-color:rgb(61,133,198)\">in</font></b><font color=\"#0b5394\">profile</font></font></a><font face=\"Verdana\" style=\"font-size:10pt\"><br></font></span></div></div></div></span></div></div></div></div></div>\r\n\r\n<br>\r\n<div dir=\"ltr\" style=\"background-color:rgb(255,255,255)\"><div dir=\"ltr\"><div dir=\"ltr\"><div dir=\"ltr\"><div dir=\"ltr\" style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:11px;color:rgb(0,0,0)\"></div><p><font color=\"#808080\" face=\"Verdana\" size=\"2\"><img src=\"https://ci4.googleusercontent.com/proxy/e6V3E5U5l5e0xH2os1Sks-xfQjBYMWwJOO5sgcpNB190WDnylH_uKYihXj2O2m1cIBbhstXNdMDcJUY8uAQvnSGQLiLMJENd-NfkIYgAPP31xQWC5bb0EuUKtnkhYhcO-HZOJgHl25DrccD_PK5iCd8CaC7Xxw=s0-d-e1-ft#https://cdn1.pegasaas.io/41fc/img/wp-content/uploads/2020/11/SELISE-DIgital-Platforms----145x60.png\"><br></font></p><p><font color=\"#808080\" face=\"Verdana\" size=\"2\">Secure Link Services Group<br>Zürich: The Circle 37, 8058 Zürich-Airport, Switzerland<br>Munich: Tal 44, 80331 München, Germany<br>Dubai: Building 3, 3rd Floor, Dubai Design District, Dubai, United Arab Emirates<br>Dhaka: Midas Center, Road 16, Dhanmondi, Dhaka 1209, Bangladesh<br>Thimphu: Bhutan Innovation Tech Center, Babesa, P.O. Box 633, Thimphu, Bhutan<br><br>Visit us: <a href=\"http://www.selise.ch\" target=\"_blank\">www.selise.ch</a><br></font></p></div></div></div></div>\r\n<br>\r\n<div dir=\"ltr\" style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:11px\"></div><div dir=\"ltr\" style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:11px\"></div><p><i><font size=\"1\"><strong>Important Note:</strong> This e-mail and any attachment are confidential and may contain trade secrets and may well also be legally privileged or otherwise protected from disclosure. If you have received it in error, you are on notice of its status. Please notify us immediately by reply e-mail and then delete this e-mail and any attachment from your system. If you are not the intended recipient please understand that you must not copy this e-mail or any attachment or disclose the contents to any other person. Thank you for your cooperation.</font></i><br></p>"
def send_message(subject:, to_address:, message:)
mail(
to: to_address,
subject:,
body: message,
)
end
My ActionMailer BaseClass looks like:
class ApplicationMailer < ActionMailer::Base
default content_transfer_encoding: '7bit'
layout 'mailer'
end
Now when I do mail.to_s
, it encodes the message with Content-Transfer-Encoding: quoted-printable
instead of 7bit
. However, it encodes the message with 7bit
if the @message
text is shorter.
Date: Wed, 24 Aug 2022 14:33:56 +0600
From: graph-api-mailer@test.ch
To: ["graph-api-mailer@test.ch"]
Message-ID: <3f8735c7-a2eb-4ca7-a208-2de0145c7b01@selise.ch>
In-Reply-To: <>
References: <>
Subject: How to hack?
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8">=
<div dir=3D"ltr">Hello <div><br></div><div>This is the first email<b=
r clear=3D"all"><div><div dir=3D"ltr" class=3D"gmail_signature" data-smar=
tmail=3D"gmail_signature"><div dir=3D"ltr"><span><p style=3D"color:rgb(34=
,34,34);line-height:1.2;margin-top:0pt;margin-bottom:0pt"><br></p><div st=
yle=3D"color:rgb(34,34,34)"><br></div><div style=3D"color:rgb(34,34,34)">=
<font face=3D"verdana, sans-serif">Regards</font></div><p style=3D"color:=
rgb(34,34,34);line-height:1.2;margin-top:0pt;margin-bottom:0pt">--</p><di=
v style=3D"color:rgb(34,34,34)"><div dir=3D"ltr"><div dir=3D"ltr"><p styl=
e=3D"line-height:1.2;margin-top:0pt;margin-bottom:0pt"><font face=3D"verd=
ana, sans-serif"><span style=3D"font-size:10pt;color:rgb(11,83,148);verti=
cal-align:baseline;white-space:pre-wrap">Md. Imam Hossain</span><span sty=
le=3D"font-size:10pt;color:rgb(11,83,148);vertical-align:baseline;white-s=
pace:pre-wrap">, B.Sc.(Engg.)</span><br></font></p><p style=3D"line-heigh=
t:1.656;margin-top:0pt;margin-bottom:0pt"><font color=3D"#999999" face=3D=
"verdana, sans-serif"><span style=3D"font-size:13.3333px;white-space:pre-=
wrap">Software Engineer</span></font></p><font face=3D"verdana, sans-seri=
f"><span style=3D"font-size:10pt;color:rgb(153,153,153);vertical-align:ba=
seline;white-space:pre-wrap">Mobile: </span><span style=3D"font-size:10pt=
;color:rgb(11,83,148);vertical-align:baseline;white-space:pre-wrap">+8801=
687242792</span></font></div><div dir=3D"ltr"><span style=3D"color:rgb(11=
,83,148);vertical-align:baseline;white-space:pre-wrap"><a href=3D"https:/=
/www.linkedin.com/in/imam07/" style=3D"color:rgb(17,85,204);display:inlin=
e" target=3D"_blank"><font style=3D"display:inline" face=3D"verdana, sans=
-serif"><font color=3D"#0b5394">Linked</font><b><font color=3D"#ffffff" s=
tyle=3D"background-color:rgb(61,133,198)">in</font></b><font color=3D"#0b=
5394">profile</font></font></a><font face=3D"Verdana" style=3D"font-size:=
10pt"><br></font></span></div></div></div></span></div></div></div></div>=
</div>
<br>
<div dir=3D"ltr" style=3D"background-color:rgb(255,255,255)"><div dir=3D"=
ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr" style=3D"font-fam=
ily:Verdana,Arial,Helvetica,sans-serif;font-size:11px;color:rgb(0,0,0)"><=
/div><p><font color=3D"#808080" face=3D"Verdana" size=3D"2"><img src=3D"h=
ttps://ci4.googleusercontent.com/proxy/e6V3E5U5l5e0xH2os1Sks-xfQjBYMWwJOO=
5sgcpNB190WDnylH_uKYihXj2O2m1cIBbhstXNdMDcJUY8uAQvnSGQLiLMJENd-NfkIYgAPP3=
1xQWC5bb0EuUKtnkhYhcO-HZOJgHl25DrccD_PK5iCd8CaC7Xxw=3Ds0-d-e1-ft#https://=
cdn1.pegasaas.io/41fc/img/wp-content/uploads/2020/11/SELISE-DIgital-Platf=
orms----145x60.png"><br></font></p><p><font color=3D"#808080" face=3D"Ver=
dana" size=3D"2">Secure Link Services Group<br>Z=C3=BCrich: The Circ=
le 37, 8058 Z=C3=BCrich-Airport, Switzerland<br>Munich: Tal 44, 8033=
1 M=C3=BCnchen, Germany<br>Dubai: Building 3, 3rd Floor, Dubai Desig=
n District, Dubai, United Arab Emirates<br>Dhaka: Midas Center, Road=
16, Dhanmondi, Dhaka 1209, Bangladesh<br>Thimphu: Bhutan Innovation=
Tech Center, Babesa, P.O. Box 633, Thimphu, Bhutan<br><br>Visit us: =
;<a href=3D"http://www.selise.ch" target=3D"_blank">www.selise.ch</a><br>=
</font></p></div></div></div></div>
<br>
<div dir=3D"ltr" style=3D"font-family:Verdana,Arial,Helvetica,sans-serif;=
font-size:11px"></div><div dir=3D"ltr" style=3D"font-family:Verdana,Arial=
,Helvetica,sans-serif;font-size:11px"></div><p><i><font size=3D"1"><stron=
g>Important Note:</strong> This e-mail and any attachment are confid=
ential and may contain trade secrets and may well also be legally privile=
ged or otherwise protected from disclosure. If you have received it in er=
ror, you are on notice of its status. Please notify us immediately by rep=
ly e-mail and then delete this e-mail and any attachment from your system=
. If you are not the intended recipient please understand that you must n=
ot copy this e-mail or any attachment or disclose the contents to any oth=
er person. Thank you for your cooperation.</font></i><br></p>=
I send it like this afterwards:
MicrosoftClient::Mail::Send.call(mail: Base64.encode64(mail.to_s)) # own class to send through microsoft graph api
When I send this mail, the received mail does not render correctly in gmail/outlook.
I have tried with Content-Transfer-Encoding: 'binary'
, but that doesn't work when I add attachments.
How can I send this email correctly so that encoding is 7bit or the output does not break in the mail clients?
Fixed with
mail(
to: to_address,
subject: subject,
content_type: 'text/html'
) do |format|
format.html(content_transfer_encoding: 'base64') { Base64.encode64(@message) }
end
Here, transfer encoding should be base64
, previously was trying with binary