New line works for Gmail but not Outlook - email

Here's the code I'm using to send an email. It works perfectly when the email goes to Gmail (it sends four short tables with a line break between each one). However, when it goes to Outlook the second table (1,16) doesn't have new lines between the table entries while the other three tables still do (I don't know if it's related to those values specifically since the tables are identical in format). I understand there's probably some difference between email services, but is there a simple way to have the emails be consistent with new lines when arriving in both places? Thank you.
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet4=ss.getSheetByName('Copy');
var emailAddress = sheet4.getRange(2,12).getValue();
var subject = sheet4.getRange(2,13).getValue();
var message = sheet4.getRange(1,15,sheet4.getRange(31,15).getValues(),1).getValues().join('\n')+
sheet4.getRange(1,14,3,1).getValues().join('\n')+
sheet4.getRange(1,16,sheet4.getRange(31,16).getValues(),1).getValues().join('\n')+
sheet4.getRange(1,14,3,1).getValues().join('\n')+
sheet4.getRange(1,17,sheet4.getRange(31,17).getValues(),1).getValues().join('\n')+
sheet4.getRange(1,14,3,1).getValues().join('\n')+
sheet4.getRange(1,18,sheet4.getRange(31,18).getValues(),1).getValues().join('\n');
MailApp.sendEmail(emailAddress, subject, message);
}

Related

Capture HTML Form Responses in Google Sheets and send a Confirmation Email to the Form Submitter

I want the script to send the created html email template email.html to the person who last submitted the form.
Below is the success message I get in the console:
Below is the actual email that is received by the person who submits the form:
The email.html is formatted correctly and appears perfectly when sent manually.
I hope this is reprex enough.
EDIT: Maybe it's easier if I include the code
function sendEmail () {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var data = ss.getRange(lr,3);
var email = data.getValue();
var message = HtmlService.createTemplateFromFile('email');
var subject = "Test Subject"
GmailApp.sendEmail(email,subject, message);
}
It's not clear why you are using HtmlService.createTemplateFromFile, but from the image it's clear there at least one error, the script misses two methods:
HtmlService.HtmlTemplate.evaluate() to evaluate the Templated HTML
HtmlService.HtmlOutput.getContent() to get the HTML from the HtmlService.HtmlOutput object returned by the previous method.
Another option that looks to be an error is the use of GmailApp.sendEmail(string,string,string) method, as the third parameter should be a string to be used as the email plain text content. If you want to pass HTML, instead use GmailApp.sendEmail(string,string,string, Object)
Related
Emailing Google Sheet range (with or without formatting) as a HTML table in a Gmail message
Sending an email in HTML and plain with a Gmail Apps Script
Google script inject html into html email
References
https://developers.google.com/apps-script/guides/html
https://developers.google.com/apps-script/guides/html/templates
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)

how can I force Google App Script MailApp.sendEmail to use a new thread for each email in a loop?

I am doing a simple app, I have an spread sheet with a few columns, one is an email addres and the others are filled with the information I want to send in an email to that addres. The code is really simple, it is just a while loop with the execution of MailApp.sendEmail with the data in each row.
The issue that I am having is that when I execute the app all the emails end up in the same email threard. And this makes really difficult to follow the responses beacause each email is independet. I've been looking for a while on how to force MailApp.sendEmail to start a new thread, but I can't find how to do it in the documentation or in the web.
The issue is that you are using the exact same subject for these emails and therefore they end up in the same email thread.
For example:
This will put all the emails in the same thread:
function myFunction() {
for (let i=0; i<2; i++){
MailApp.sendEmail("example#gmail.com",
"Test", // subject is the same for every email
"This is an test email");
}
}
This will put the emails in different threads:
function myFunction() {
for (let i=0; i<2; i++){
MailApp.sendEmail("example#gmail.com",
"Test"+i, // subject is different for every email
"This is an test email");
}
}
Instead of using i which is not very well descriptive as a subject, you could send the datetime:
"Test"+new Date()
although this might not work if the emails are sent instantly and therefore the subject will be the same again, but it might work in your case.
Ideally, you would want something that is relevant to the email, for example relevant to the body of your email.

MVC5 Mailkit send email with incorrect Email address

I am trying to send emails using my MVC5 application. To do this, I have installed Mailkit v 1.22.0 through NuGet package manager. And this is how my code looks like:
var FromAddress = "no-reply#email.com";
var FromAddressTitle = "My Org";
var connection = ConfigurationManager.ConnectionStrings["SmtpServer"].ConnectionString;
var Email = new MimeMessage();
Email.From.Add(new MailboxAddress(FromAddressTitle, FromAddress));
var AddressArray = value.SentTo.Split(';');
foreach (var item in AddressArray)
{
Email.To.Add(new MailboxAddress(item));
}
Email.Subject = value.Subject;
Email.Body = new TextPart("html")
{
Text = value.Content
};
using (var client = new SmtpClient())
{
client.Connect(connection);
client.Send(Email);
}
return "Email Successfully Sent";
which works fine except if a wrong recipient Email address has been entered, the application does not detect if the Email was actually sent or not (client.Send(Email) returns void). Is there a way to know if it really ended up getting sent to the recipient or not? If it is not possible with Mailkit, is there any other NuGet package that can do this?
The reason that SmtpClient.Send() returns void is that the SMTP protocol does not specify whether the message gets delivered successfully. All it can do us tell the client that the messages was accepted by the server or not (in which case MailKit will throw an exception).
If you need to know whether the message was successfully delivered, you will need to check for bounce messages sent to you which could take minutes or even hours.
The first thing you'll have to do, however, is subclass SmtpClient and override the GetEnvelopeId and GetDeliveryStatusNotifications methods.
Then, when you receive a bounce message, the top-level MIME part will typically be a multipart/report (represented by a MultipartReport object when using MimeKit). This multipart/report will then contain a message/delivery-status MIME part (and possibly others), which will have a list of header-like fields that specify the details about the delivery status for 1 or more recipients.
MimeKit will parse a lot of this for you (e.g. it has a MessageDeliveryStatus class which contains a StatusGroups property that you will want to use. However, what MimeKit does not do is parse the individual field values (but they shouldn't be that difficult for you to do, typically a few Split(';')'s should be enough iirc for some quick & dirty parsing).
You will want to read the spec for this at https://www.rfc-editor.org/rfc/rfc3464
The MimeKit docs linked above specify which sections to look closely at (I think 2.2 and 2.3).
I would recommend looking specifically at the Original-Recipient and Action fields.
original-recipient-field =
"Original-Recipient" ":" address-type ";" generic-address
generic-address = *text
action-field = "Action" ":" action-value
action-value =
"failed" / "delayed" / "delivered" / "relayed" / "expanded"
You will also need the Original-Envelope-Id field to figure out which message is being reported on:
original-envelope-id-field =
"Original-Envelope-Id" ":" envelope-id
envelope-id = *text
The envelope-id text will be the same string returned by your GetEnvelopeId implementation in the SmtpClient class.

Sending email to multiple recipients from UWP store app

I have a simple goal, to open up an email (in Outlook 2016) with the To field configured for multiple recipients from a Windows 10 UWP app.
I tried 3 approaches
1) The recommended way, as demod in the UWP samples, using the EmailMessage
var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
emailMessage.Body = "";
foreach (Person p in SelectedPeople)
{
if (string.IsNullOrEmpty(p.Email) == false)
{
var emailRecipient = new Windows.ApplicationModel.Email.EmailRecipient(p.Email);
emailMessage.To.Add(emailRecipient);
}
}
await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(emailMessage);
This results in an email window with the recipients seperate by commas which then do not resolve. Setting the option to allow comma seperators seemed like an answer, but it tuens out that doesn't work unless there is a space to seperate too?
2) Build a mailto:user1#work.com;user2#work.com URI and launch it.
var uri = new Uri("mailto:user1#work.com;user2#work.com");
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
However, attempting to create URI with multiple recipeitns throws an exception that the URI hostname is invalid
3) Same as above but using the scheme mailto:?To=user1#work.com;user2#work.com
This is parsed correctly as a URI but on launch Outlook shows an empty recipient list. By way of testing, using CC= does show the recipients in the CC field
So, now I am stuck wondering how I can send an email to multiple recipients for a store app?
Late to the party, but someone linked this from another question..
Have you tied mailto:user1#work.com%3buser2#work.com
seems to work for me

Undefined e.values on google email notifications

I am using script editor in google spreadsheets to send an auto response email. However, I received Summary of failures notifications suggesting an invalid email: undefined (on the Mail.App line). I don't know how this error came about. Please help. Thanks so much.
function myFunction(e) {
var Nickname = e.values [2];
var email = e.values [10];
var subject = "Form Submitted"
var message =
"Thank you, " = + Nickname + ". Your response is accepted. You may want to check our website # iydsphilippines.weebly.com for registration payment details. Thanks and we hope to see you soon!";
MailApp.sendEmail(email, subject, message);
}
Here is a link to a copy of my spreadsheet.
<>
I'm no expert however I also had the same issue and by trial and error I have discovered what the problem is.
It seems that recently Google have "upgraded / changed" the way fields are evaluated.
If any fields are blank then problems arise and as your email field in column K (i.e. the 10th field counting the first as zero) and some of the others are blank it seems to get confused. Until Google fix this "undocumented feature" I have a workaround.
Move your Email column to nearer the beginning i.e. straight after the name for example but before any optional fields, then renumber it in the code and you should be OK.
Specifically in your example: Move Column K to be Column C and renumber "var email = e.values [10]" to be "var email = e.values [2]" That should do it.
Good Luck and let me know how you get on. From Lester in the UK