Mail with html content shows break lines or ignores newlines - email

I am sending mail to users via mandrill and I using both smtp and mandrill api to send.
Content of the mail is rendered go template (.tpl)
When I put template like
Hi {{.name}},
<br/>
This is support.
<br/>
it sends via mandrill api ok, but is visible when I send via smtp,
when use template like ( <br/> replaced with \n)
Hi {{.name}},
This is support.
mandrill ignores that and shows everything in one line but smtp shows ok newlines.
What is a solution for this ?
I am rendering template like
frame, err := template.New("foo").Parse( *templateString )
if err != nil {
return nil, err
}
var doc bytes.Buffer
frame.Execute( &doc, *parameters )
temp := doc.String()

Are you sending the mail as HTML? If so, you can wrap everything in the <pre> tag.
If you're not using HTML, setting this header should help: Mime-Type: text/plain
Also, try changing your newlines from \n to \r\n.

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)

Send email html body as Base64 encoded

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.

Extracting attachment from email file using GOLANG

I have been working on parsing email using golang. I am now in the part of extracting the attachments. I have looked into golang lib MIME and MIME/multipart. But it does not have any methods or function to do this.
What specifically I want to do is: Example
I have an email file with attachments file1.txt, file2.pdf, and file3.png. I have successfully parsed the email body. Now I want to extract the attachment and save them on a separate directory. I have searched all part of golang including MIME and MIME/multipart. They seem to not have this functionality. Can golang do this? if Yes any hint or clue please.
I found a solution to this that uses the parsemail function from DusanKasan
import (
"github.com/DusanKasan/parsemail"
)
func readEmail() error {
b := getYourEmail()
email, err := parsemail.Parse(bytes.NewBuffer(b))
if err != nil{
return err
}
for _, a := range email.Attachments{
// do stuff with attachment
}
}
I think firstly you should find the boundary of:
Content-Type: multipart/mixed; boundary={sample-boundary}
Then you split the email by that sample-boundary.
And finally you get the base64 encoding part of attachment.
I'm current working on this. I'll be back when I'm done.

GMail breakpoints to hide/expand an email

Description of the issue
I am sending emails in JavaScript with
message2 = message.replace("<br>", "\n")
GmailApp.sendEmail(address, subject, message2, {
cc: cc,
name: sendername,
htmlBody: message
});
…where message is a long string containing HTML tags.
On my Gmail account the email that is received contains a … in the middle to hide/extend the end of the message. The … correspond to newlines (<br>) but it seems to chose a newline randomly that will be used to split the message randomly.
I cannot get rid of all <br> of course. I always saw these … to separate the main part of the email from the signature. But in my case it just appears anywhere as for example right after the first line:
Question
How does GMail choses the “breakpoints” for shortening the message?
How can I avoid that GMail uses “breakpoints?”
Should I use something else than <br> to make newlines?

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');.