When sending mail using MailApp.sendEmail() or GmailApp.sendEmail() from App Script to a non-existent address I do not get an email informing about the error. I would expect an email to the account that sends reporting problem: 550 recipient rejected.
function testNonExistentAddress() {
var _recipient = 'nonexistentaddress#myexample.net';
var _subject = 'TEST FROM MAILAPP APPSCRIPT';
var _body = 'TEST';
MailApp.sendEmail(_recipient, _subject, _body);
}
The problem is that it is not sending it from you. It's simply bouncing it off a Google server. You will also notice that it doesn't appear in your sent folder, and that you need to declare who the email is coming from. I also bcc myself on the emails that my apps send so that I have a record in my mail account.
Nothing to be done, I believe. Other than checking the issue tracker / feature request. Or calling a third party system to ensure that the email address is real.
Related
I have a Google Sheet, its have several internal code (gs) and finish doing a list with mails and text, actually Im using a function to send mails:
function SendMail () {
var vAuxiliar = SpreadsheetApp.getActive().getSheetByName("Auxiliar");
var vLast = vAuxiliar.getDataRange().getNumRows();
var vDataRange = vAuxiliar.getRange("A2:E"+vLast);
var vValues = vDataRange.getValues();
for(var i in vValues) {
var vemailAddress = vValues[i][1];
var vMensaje = vValues[i][4];
var vSubject = 'Contact.';
MailApp.sendEmail(vemailAddress, vSubject, vMensaje);
};
};
Its works great but mails are incoming with my personal account, I need change this function to send mails with my enterprise account, we use MS Outlook, it is possible?
Thanks in advance!!!
You sure cannot use the Gmail service to send messages through your MS Outlook account.
But a possible workaround would be to create aliases for your MS Outlook account in your Gmail account, and then using this alias when sending.
GmailApp.sendEmail(to, 'Subject', 'A message sent from an alias!', {'from': "enterprise#example.com"});
But for using this from parameter in sendEmail you need to be able to retrieve it as an alias with getAliases().
Refer to this page to add your enterprise mail as an aliases to be able to use later.
Note that using the term alias here refers to the Gmail terminology as external email address. Do not check "Treat as alias" when configuring your MS Outlook account. This is another story.
Also as Ruben said in the comments you could try to ask if there are any API you can use to send mail. In that case you should try to use the URL fetch method.
I'm trying to send an email to a number of users from a Google Sheet, triggered by a menu item by the user.
Unfortunately the emails won't send if I run the script from my work email.
I checked the oauthScopes under appsscript.json to be sure that the proper scopes are requested by the script, and they are.
I've also tested the code multiple times in a personal account, and it works. However when I run the code from my work account, the emails won't send. Everything about the code in the two accounts is the same, except for the domain sending the email.
function submit() {
//gather admin emails
var admins = ["someone#example.com"]
var thisUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var thisName = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getOwner().getName();
MailApp.sendEmail(admins.join(", "), thisName + " has submitted their template for upload", thisUrl)
}
As mentioned, the code works fine from a personal account. Any ideas why it isn't running from my work account?
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 ;-)
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.
Here's my Google App Script code:
var messages = threads[x].getMessages();
for (var y in messages)
{
messages[y].forward("someone#example.com");
}
This forwards fine, but it resets the from field of message to the email id of account which executes the script. This removes all credentials of the original sender of message. How to forward the email more elegantly such that all original headers (at least from and to) are retained? If the original message was sent to a list of people or a newsgroup, I need to retain it in the forwarded message.
In general a forwarded email does not retain the original from and to address. If you forward an email using GMail or Outlook you'll notice this. The GMailApp is behaving the same way.