使用另一个“发件人”地址从Google脚本发送电子邮件 [英] Send email from a Google Script with another 'From' address

查看:98
本文介绍了使用另一个“发件人”地址从Google脚本发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单绑定脚本使用MailApp.sendEmail发送电子邮件。



电子邮件始终从我自己的Gmail帐户发送。这个项目是针对一个客户的,我需要他的电子邮件是这些电子邮件的'发件人'。



阅读类似的问题,我知道我只有改变的灵活性名称和replyTo地址,使用MailApp.sendEmail的高级选项。但是,该电子邮件地址仍然是我的,Google不提供任何控制权。



我不太熟悉所有Google服务和选项最好的方法来做到这一点。我的客户确实有Google Apps for Business,但我没有。



我能以某种方式将邮件发送功能创建为他帐户下的独立脚本,并以某种方式从我的帐户下的项目调用它?
其他想法?



谢谢!

解决方案

电子邮件始终是从执行脚本的用户帐户发送的。如果电子邮件是通过触发功能发送的(可安装触发器是唯一可以发送电子邮件的触发器,因为它需要明确的授权),那么电子邮件将从创建触发器的用户帐户发送(并授权)。
在你的情况中,似乎更简单的解决方案是要求你的客户自己执行脚本并自己启动所有触发器。
如果不可能,那么你确实可以使用一个独立的脚本,它可以作为一种代理工作,也就是说,它会收到一个请求,发送一条消息,并实际从客户帐户发送它,同时返回一个确认你的脚本。
但这有点棘手......第一个解决方案更优雅。






编辑:



我发现通过一个独立的脚本发送电子邮件的想法很有趣,所以我给它一个快速尝试,它似乎很容易地完成这项工作...测试下面的代码(此代码应该从您的客户帐户中部署为独立应用程序):

$ p $ 函数doGet(e){
Logger.log 'e = e'+ JSON.stringify(e));
if(e.parameter.recipient == null){return ContentService.createTextOutput(error,wrong request+ JSON.stringify(e)+\\\
\\\
+ e.parameter.recipient + \\\
\\\
+ e.parameter.subject + \\\
\\\
+ e.parameter.body).setMimeType(ContentService.MimeType.TEXT)};
尝试{
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);

$ / code>

注意:下面的代码放在您的电子表格脚本中,上面的doGet是独立的应用程序运行从您的客户帐户。

  function sendMail(recipient,subject,body){
var sent = UrlFetchApp .fetch( https://script.google.com/macros/s/---------------S381kO1Kqv61E/exec?recipient= +接受者+ &安培受试者= + +受试者&安培;身体= +体);
Logger.log(发送);
}

函数test(){
sendMail('eatient@gmail.com','测试消息','hello world')
}

我能够从我的Gmail帐户发送消息,同时以不同的用户身份登录。 (上面的url有意截断,我不希望任何人从我的账户发送邮件; - )


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

这篇关于使用另一个“发件人”地址从Google脚本发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆