MailApp.sendEmail function gives a generic error - google-apps-script

I have a budget sheet, which need to be populated in africa, by people without a google account.
I share the google sheet via a link and they can update the content. When they finished the updating, I would like to give them a possibility to send it to a small distribution list via a script
function sendReport() {
var message = {
to: "user#domain.org",
subject: "Budget proposal",
body: "Hi team,\n\nPlease find the budget attached.\n\nThank you,\n",
name: "Budget",
From: 'user#gmail.com',
attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Budget")]
}
MailApp.sendEmail(message);
}
This works fine as long as I m logged in via any google account, but for a non google account, I get the following error:
Script sendReport experienced an error Details
The amount of data is too much for a form (but in a form I seem to be able to ask them to send a mail, so I wonder where I go wrong...
Thanks

MailApp.sendEmail(message) does not accept a From parameter, so that part of the object message gets ignored.
So if your account executes this script, you send it from your own domain email. Since it requires a google email to send a message, non-google accounts cannot execute this script.
Reference:
MailApp.sendEmail

Related

MailApp returns a delivery failure with Gsuite account, but not with Gmail

I have tested a Google Apps script to send a google doc converted to PDF by mail automatically. The email is sent to one email address at a time.
I tested this with my normal Gmail account and it worked fine.
But when I try to use it on my Gsuite account (professional mail), I get a "delivery failure" mail with this error inside it :
Message blocked
Message rejected. See https://support.google.com/mail/answer/69585 for more information.
After calling the google assistance, I activated the "allow unsafe application access". It's been 4 hours now since I've activated this option (the option says that it might take some time to get into effect), but the error is still there.
Do you know why such an error may be generated ?
The link provided in the delivery failure mail does not provide precise information about this mail, apart from general indications like maybe it's "spam", "the recipient does not exist", etc...
Such indications does not qualify with this mail because the fact is that I know it's usually working on my Gmail account. I don't know why it's blocked with the Gsuite account.
EDIT : When I send the same mail manually with the Gsuite account, with the same title, body and attachment, the mail goes through without any issue.
I have to point that I did not shared documents/sheets or the script between the two accounts (because it has generated weird script behavior in the past). I copy-pasted the documents and the scripts in my new Gsuite account. My Gsuite account is the only owner and has the only access to the documents and the script, without any sharing.
Here is the portion of the script used to send the automated email :
//recipientmail is defined elsewhere
//newPDFReportId is defined elsewhere
if (recipientmail !== null) {
MailApp.sendEmail(
recipientmail,
"EMAIL OBJECT",
"Hi !\r\rThis is an example of an automated mail.\r\r\rDon't hesitate to get in touch.\rThis is the alpha version of the email sender.",
{attachments: DriveApp.getFileById(newPDFReportId)})
}
I solved the delivery failure of the automated mail by using GmailApp instead of MailApp in my script.

How do I get specific Gmail message parameters to be sent to a Google Sheet?

I checked out the following question which has an example relating Gmail and Google Sheets: How to use Google App Scripts to retrieve Gmail emails in a customised way?
I'm wondering how I could add additional parameters: message date, sender email, message subject, full message body.
The problem with the above script is it retrieves the last email address from any thread in the inbox. I'm concerned with threads as well as individual messages.
Last, I'd love to add a clause that specifies to only grab message data for those messages that I've tagged in Gmail.
Here's some documentation on Gmail API and Google Sheets.
I think what you need here is Google Apps Script. So by checking the documentation, you can find the different methods that you can do in Gmail message.
For your question:
How I could add additional parameters: message
date, sender
email,
message
subject,
full message
body.
As you can see in the above document. You will find here the method getDate() that will get the date and time of the message.
var thread = GmailApp.getInboxThreads(0,1)[0]; // get first thread in inbox
var message = thread.getMessages()[0]; // get first message
Logger.log(message.getDate()); // log date and time of the message
Check the other link for some sample code of each parameter.
Also just check the documentation of AppScript on how to send this to a Spreadsheet.
For Question How I could add additional parameters: message date, sender email, message subject, full message body.
refer this documentation.
For App-script on how to send this to a Spreadsheet.
First create Spreadsheet in drive refer below code to add to it.
var ss = SpreadsheetApp.open(DriveApp.getFilesByName('ADD NAME').next()).getSheets()[0]; ss.appendRow(["RESULT"])

Send email from a Google Script with another 'From' address

My Sheet-bound script is sending an email using MailApp.sendEmail.
The emails are always sent 'from' my own Gmail account. This project is for a customer, and I need his email to be the 'from' on these emails.
Reading similar questions I learn that the I only have flexibility in changing the name and replyTo address, using the advanced options of MailApp.sendEmail. However, the email address is still mine and Google doesn't offer any control over that.
I'm not familiar enough with all of the Google services and options to find the best way to do this. My customer does have a Google Apps for Business, but I don't.
Can I somehow create the email-sending function as a standalone script under his account, and somehow call it from the project under my account?
Any other ideas?
Thanks!
Emails are always sent from the account of the user that executes the script. In case the email is sent by a triggered function (installable triggers are the only ones that are able to send emails since it requires explicit authorization) then the email is sent from the account of the user that created the trigger (and authorized it).
In your case, it seems that the easier solution would be to ask your customer to execute the script himself and initiate all the triggers himself too.
If that should not be possible then you could indeed use a standalone script that would work as a kind of proxy, ie it would receive a request to send a message and actually send it from the customer account while returning an acknowledgement to your script.
But that's a bit tricky... the first solution is more elegant.
Edit :
I found the idea of sending emails through an independent script funny so I gave it a quick try and it seems to do the job pretty easily... test code below (this code should be deployed as a standalone app from your customer account) :
function doGet(e) {
Logger.log('e = e'+JSON.stringify(e));
if(e.parameter.recipient==null){return ContentService.createTextOutput("error, wrong request "+JSON.stringify(e)+"\n\n"+e.parameter.recipient+"\n\n"+e.parameter.subject+"\n\n"+e.parameter.body).setMimeType(ContentService.MimeType.TEXT)};
try{
MailApp.sendEmail(e.parameter.recipient, e.parameter.subject, e.parameter.body)
}catch(err){
return ContentService.createTextOutput('error : '+err).setMimeType(ContentService.MimeType.TEXT);
}
return ContentService.createTextOutput('mail successfully sent').setMimeType(ContentService.MimeType.TEXT);
}
note : the code below goes in your spreadsheet script, the doGet above is an standalone app running from your customer account.
function sendMail(recipient,subject,body){
var sent = UrlFetchApp.fetch("https://script.google.com/macros/s/---------------S381kO1Kqv61E/exec?recipient="+recipient+"&subject="+subject+"&body="+body);
Logger.log(sent);
}
function test(){
sendMail('recipient#gmail.com','test message','hello world')
}
I was able to send messages from my gmail account while being logged as a different user. (the url above is intentionally truncated, I don't want anyone to send email from my account ;-)

MailApp.sendEmail Not Working?

I have several scripts in Google Docs spreadsheets that use MailApp.sendEmail to send e-mails incorporating data from the spreadsheets. They've all worked marvelously for years. About 2 weeks ago, they stopped sending the e-mails. The scripts still run and I don't get any errors. The e-mails just never come through. Nothing has changed, except that the e-mails aren't arriving anymore.
Any thoughts?
Thanks!
I think I have an answer to my own question, which may be helpful to anybody else who has this problem. MailApp.sendEmail DOES, indeed, still seem to be working. What has changed is the way Gmail is handling certain types of messages.
If I use MailApp.sendEmail to send an e-mail to a third party, it'll get through, no problem. As pointed out by Fashtas, if I use MailApp.sendEmail to send an e-mail to the Gmail account that the spreadsheet belongs to, no problem.
Here's the problem: if I use MailApp.sendEmail to send the message to a third party account that automatically forwards BACK to the Gmail account that the spreadsheet belongs to, those messages no longer get through.
Therefore, in the sample code I posted above, the matt#.com e-mail address automatically forwards back to my Gmail account. The messages sent by the MailApp.sendEmail make it through to the .com mail server, but for some reason, they don't get forwarded back to my Gmail inbox. They appear in the Gmail Sent Items folder (presumably because they were sent by the Gmail account that the spreadsheet belongs to), but they never hit the inbox.
That behavior is new. In the past, there has been no problem with those messages getting forwarded from the *.com server to my Gmail inbox. I don't know what changed to cause the problem. And I don't know how to fix it.
But I, therefore, think the answer to my question is that MailApp.sendEmail IS working as designed.
As explained by doebtown, MailApp.sendEmail() will refuse to send messages to any address that forwards to your account. For example, if you (the spreadsheet's owner) belong to a group, people#myorg.com, within your organization then mailApp.sendEmail() messages will not be delivered to people#myorg.com. That is, the following code will not send:
var email = 'people#myorg.com';
var subject = 'The Week Ahead';
var body = 'Hi there!';
var htmlbody = "<p>Hi there!</p>";
// this will not send
MailApp.sendEmail(email, subject, body,{
htmlBody : htmlbody
});
HOWEVER, you can work around this issue by appending your email to the addresses.
var email = 'myaddress#myorg.com, people#myorg.com';
var subject = 'The Week Ahead';
var body = 'Hi there!';
var htmlbody = "<p>Hi there!</p>";
// this will send to your address and the full list
MailApp.sendEmail(email, subject, body,{
htmlBody : htmlbody
});
This email will send to your address as well as to the list to which you belong. This is not a perfect solution, but is viable if you do not want to create a specific account for managing scripts.
You can apparently only send emails to the gmail/email account the spreadsheet belongs to.
There are multiple ways to send an email through Google App Scripts.
MailApp: Traditional method, however I could not get a single email to send. Introduction course here.
GmailApp: Same method as MailApp however using the below code worked to send emails.
GmailApp.sendEmail(emailList[index], "New email", "New email", {"cc":"XXXXXXX#gmail.com"});
Add a the Gmail API through Services. This options was not explored but documentation can be found here.
Conclusion: Don't use the MailApp, instead use GmailApp.

Google script mailapp.sendEmail only works with my email address

Okay this is very strange. I have scripting in a spreadsheet to take information from the currently active sheet, creates a report using a template file and sends it as an attachment.
This all works perfectly, when I use my Google apps domain account email address as the recipient. It doesn't work when I send to any other email address, other than my own. It was working yesterday. No errors generated when the script runs
The only thing that I did was change the owner of the spreadsheet to another user in our domain. It was shared with the other user while I was testing the scripts. I've tried using other email addresses in our domain and created a new spreadsheet with the sendemail function, all with the same behavior.
// Email the specified report
function emailReport(file, recipients, emailSubject, emailMessage) {
MailApp.sendEmail("someone#example.com", emailSubject, emailMessage,
{attachments: file, mimetype: 'application/pdf'});
}
I've been able to reproduce the problem and to find a workaround.
As it's not posible to send email to recipients without google account, I add that kind of emails on a cc field.
// Email the specified report as cc
function emailReport(file, recipients, emailSubject, emailMessage) {
MailApp.sendEmail("my#gmail.com", emailSubject, emailMessage,
{attachments: file, mimetype: 'application/pdf', cc:"someone#example.com"});
}
I noticed this question a while back, and even referenced it in another one. Since then I've noticed no one responded to you so...
It appears as if Google has recently changed (though not documented anywhere I've found) the MailApp.sendEmail function so that it only works when you use the email address belonging to the owner of the spread sheet.
My guess is this is to prevent the system being used for unsolicited mass emailing.
The other question was here
(Sorry, not an answer as such :( more of a confirmation as to what you are seeing seems to be as expected)
For anyone having this issue, use the GmailApp class instead of the MailApp class.
It supports the same functions, for example: GmailApp.sendMail('recipient#gmail.com', 'Subject', 'Message').
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)