Send email html body as Base64 encoded - mailkit

How do I in Mimekit send an email having the html body base64 encoded?
In code I first create the entire body as a MimeEnitity including attachments using the BodyBuilder. Then I create the MimeMessage to be sent having the body.

The first thing you'll need to do is to locate the HTML body part.
A quick hack might look something like this:
var htmlBody = message.BodyParts.OfType<TextPart> (x => x.IsHtml).FirstOrDefault ();
Then you'll need to set the Content-Transfer-Encoding:
htmlBody.ContentTransferEncoding = ContentEncoding.Base64;
That's it.

Related

How to get the correct formatting in an Email when using Google Sheets and App Script

I'm trying to send a bunch of mails to a list of contacts that I have saved in Google Sheets.
I copied the code to send the emails, but the emails that are sent aren't in the format that I need them in, and are getting converted to plaintext when being sent.
Any help on how to get around this issue?
P.S. My coding knowledge is nil, and I just copied the code from this website : https://productivityspot.com/automatically-send-emails-from-google-sheets/
The code I used is below:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Sheet1');
var sheet2=ss.getSheetByName('Sheet2');
var subject = sheet2.getRange(2,1).getValue();
var n=sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++){
var emailAddress = sheet1.getRange(i,2).getValue();
var name=sheet1.getRange(i,1).getValue();
var message = sheet2.getRange(2,2).getValue();
message=message.replace("<name>",name);
MailApp.sendEmail(emailAddress, subject, message);
}
You are facing a couple hurdles here. The only way you can send a formatted email is with an HTML body. HTML is the markup language used to display a webpage in your web browser, and it can also be displayed by email clients.
So in order to send a nicely (HTML) formatted email from Apps Script, you must first generate the HTML formatted body, then you must include is using the htmlBody parameter. You should also always include a plain text body (as you are now), just incase you have a recipient who doesn't receive HTML email (common for recipients with accessibility needs, using screen readers etc.)
There is no simple way to fetch formatted text from a spreadsheet cell as HTML, so you will need to either store your raw HTML in the spreadsheet, or figure out another location you can store and fetch the HTML body from. In my example below, I hardcode both the plain text and html bodies.
To send an HTML Body with MailApp, you need to use the additional "options" parameter, as shown below.
var html_message = '<body><h1>This is an HTML formatted body</h1><br><br><strong style="color:red">This can get pretty involved.</strong> Usually you would generate this html with some other tool that lets you nicely format your content and export it as HTML.</body>'
var plain_message = 'This is a plain text email body. It is is much simpler, with no special formatting, but usually it will include all the text from the html formatted message (above), since usually you want all your recipients to receive the same information'.
MailApp.sendEmail(emailAddress, subject, plain_message, {htmlBody: html_message});
See the advanced options specification here:
https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)

Is it possible to create MimeMessage from String in flutter?

I fetched emails from gmail server using pop3 with enough_email package. Everything works as expected until I updated the flow like fetch emails which is encrypted by PGP encryption. I already achieved the decryption part of email using OpenPGP package which gives String in this form
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="000000000000da67ad05d5c5dfba"
--000000000000da67ad05d5c5dfba
Content-Type: text/plain; charset="UTF-8"
test 3
--000000000000da67ad05d5c5dfba
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">test 3</div>
--000000000000da67ad05d5c5dfba--
now I am trying to create mimeMessage object from the string so I can parse it using decodeTextPlainPart function. Already tried to create mimeMessage object by using MimeMessage.parseFromText function but it's not working as expected, when ever I try to get plain text using decodeTextPlainPart function from my created mimeMessage object it returns the same thing which I provided to MimeMessage.parseFromText function.
Did I miss something or followed the wrong approach?
OpenPGP.decrypt function return string with \n as line separator but MimeMessage.parseFromText works with \r\n, when I replace all \n with \r\n in string then try to create mimeMessage object from the string I get the expected result.

Zend Framework Mail: Base64 header encoding on incoming mails

I try to download emails from my POP3/IMAP accounts using Zend Framework 1.12 and it's working fine. QP header fields will be decoded automatically. However, when a header field (from name or subject) is base64 encoded like this:
=?UTF-8?B?c3DEvsWIYcWl?=
it will not automatically base64 decode it. Don't know why. While it would be easy to fix this "my way", I would like to do it right.
Can anybody recommend a good approach how to deal with base64 headers?
Thanks a lot.
You can use use iconv_mime_decode_headers() PHP function.
$decoded = iconv_mime_decode_headers('Subject: '.$subject, 0, "UTF-8");
var_dump(decoded['Subject']);
Note, that you can pass multiple header parameters to one function, by separating them with newline or "\n". e.g.
$headers = "Subject: {$subject}\nFrom: {$from}";
$decoded = iconv_mime_decode_headers($headers, 0, "UTF-8");
In this case you will get array with keys "Subject" and "From" with decoded data.
Its the responsibility of mail mime parsers to decode the mail headers. There are open source base64 decoders available on net which can be used to decode these strings.

How can I send a multi-part email with text/plain and text/html parts with Grails?

I've looked through the code and documentation for the Grails Mail plugin (version 0.9) and it doesn't have the support I'm looking for. You can only set a single body and then provide a mime attachment that points to a static file. I need to actual pass a model into a GSP and have it render both the HTML and plain text versions and then have those both available in the message. This will allow non-HTML-based e-mail clients to display the text/plain part and clients that support HTML to display the text/html part.
Has anybody done this with Grails? Is there an easy way to do it, or do I have to modify the mail plugin or just go to the Java Mail library directly?
Since version 1.0 the mail plugin natively supports multipart alternative content as described in http://jira.grails.org/browse/GPMAIL-37
mailService.sendMail {
multipart true
to <recipient>
subject <subject string>
text 'my plain text'
html '<html><body>my html text</body></html>'
}
We use multipart email with the standard email plugin. The following code snippet is located in a service class, that's why we're using standard groovy templating instead of the gsp engine:
Template template = groovyPagesTemplateEngine.createTemplate(<templatename>)
Writable emailBody = template.make(<data model as map>)
StringWriter bodyWriter = new StringWriter()
emailBody.writeTo(bodyWriter)
String xml = <some xml>
mailService.sendMail {
multipart true
to <recipient>
subject <subject string>
body bodyWriter
attachBytes "filename.xml", "text/xml", xml.getBytes('UTF-8')
}
The crucial thing is that 'multipart true' appears at the beginning of the closure. If you add
html '<b>Hello</b> World'
to the closure above, I assume you'll get a text and html email with an attachment.
Seems like this is potential content of version 1.0 of the Mail plugin, see this and this issue. Looking at the patch of the first issue, I think an html and text multipart message could simply be created like this:
mailService.sendMail {
multipart true
to <recipient>
subject <subject>
dualBody(template:<template>, model:<model>)
}
Would be pretty cool! No idea if / when this will be released though.

Sending an HTML email using Swift

I would like to send an auto-generated email with HTML body from my application using Swift.
Here is my current code:
$message = Swift_Message::newInstance()
->setFrom(array('dummy1#test.com' => 'John Doe'))
->setTo('dymmy2#test.com')
->setSubject('some subject');
$message->setBody($this->getPartial('global/mail_partial'));
$this->getMailer()->send($message);
I had already tried to change the header Content-type of the email message using some specific Swift methods but it is not working.
See:
Sending a HTML E-Mail (from SwiftMailer Docs)
You need to add this line to set html content-type:
$message->setContentType("text/html");
Alternatively, it can by done passing a second argument on the $message->setBody() method:
$message->setBody($this->getPartial('global/mail_partial'), 'text/html');.