如何在应用程序脚本发送的电子邮件中显示我作为发件人的姓名? [英] How do I get to show my name as sender in the emails send from app script?

查看:0
本文介绍了如何在应用程序脚本发送的电子邮件中显示我作为发件人的姓名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码现在如下所示:

GmailApp.sendEmail(address, subject, body, {htmlBody : body});

我的参考问题: Syntax Error for adding signature to email created from sheets using apps script for

我的问题: 它将发件人显示为alex.ken@...

我想要什么:我希望脚本使用用户的名称。所有用户都在Google设置中为其电子邮件地址设置了不同的名称。

PS-请注意,我有多个用户在使用工作表/宏,因此名称参数中不能有固定值。

请帮帮忙。

编辑1=解决方案:

var user = Session.getActiveUser().getUsername(); //gives username
var firstpart = user.split('.')[0]; //gives first part of username
var uppercase = firstpart.charAt(0).toUpperCase() + firstpart.substring(1); //makes first letter upper case
var display = uppercase + " with Ban Company";
.
.
.
.
GmailApp.sendEmail(address, subject, body, {htmlBody : body + signature,cc: "test@gmail.com",name:display});

推荐答案

您的解决方案适用于name.surname@...格式的电子邮件,但如果用户使用其他格式,如itadmin@...、昵称或任何其他电子邮件变体,则可能会遇到问题。

另一种解决方案是使用People API Advanced Services检索当前用户的配置文件名称,因为Apps脚本的Session.getActiveUser()仅包括用户的电子邮件,并且存在某些情况where it won't work

这包括更多步骤,他们必须授权People API,但您可以获取任何用户的配置文件名。

  1. Enable the Advanced Service在应用程序脚本编辑器中,单击Services旁边的+。然后,您可以添加人员服务。

  1. Add the profile scope to your project通过编辑appscript.json清单文件。
  {
    ...
    "oauthScopes": [
      "https://www.googleapis.com/auth/userinfo.profile"
    ],
   ...
  }
  1. 添加将从People API检索当前用户的配置文件名称信息的函数。
//This will contain a user's names
function getCurrentUser(){
  var currentuser = People.People.get("people/me", {"personFields":"names"})
  return currentuser.names[0]
}

var firstname = getCurrentUser().givenName //gets the user's first name
var fullname = getCurrentUser().displayName // gets the user's full profile name

//You can then add either variable as the name 

GmailApp.sendEmail(address, subject, body, {htmlBody : body + signature,cc: "test@gmail.com",name:firstname});


如果您需要它,您还可以检索更多信息,不仅是姓名,还包括其他用户配置文件信息。查看文档here

这篇关于如何在应用程序脚本发送的电子邮件中显示我作为发件人的姓名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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