首页 > 解决方案 > Google Apps 脚本不发送电子邮件(内部发送除外)

问题描述

让我首先感谢任何试图提供帮助的人。现在,问题:

我有一个执行以下操作的脚本(好吧,不是真的,因为第一部分失败了):

当我运行脚本(使用“初始化”功能)时,我在我的个人帐户上收到一封电子邮件(作为恢复和辅助电子邮件)链接到工作帐户(因为它是在本地传递的,而不是使用 SMTP 发送的,正如消息所清楚证明的那样header),但组中没有其他联系人收到电子邮件。由于这是唯一失败的部分,我猜这个问题一定与“GmailApp.sendEmail”功能有关(尽管它可能与获取指定组中的联系人的一些问题有关),但到目前为止,经过几天的故障排除,我无法找到解决方案... :-(

如果有人可以提供帮助,我将不胜感激。并提前一千感谢!

快速更新:所以,我找到了一种测试结果的方法,似乎问题与我的 Google 联系人联系人有关。Contacts[i].getPrimaryEmail() 的输出显示除一个联系人(带有我的恢复电子邮件的那个)之外的所有联系人为空:

联系人

null
null
null
nuno*******da@gmail.com
null
null
null

附加脚本文件“Code.gs”和“form.html”内容。


function Initialize() {
  
  var NAME  = "Mark Twain";           // It will show up in the signature of your outgoing emails
  var CONTACTGROUP = "Test1";     // Enter the exact name of your Google Contacts group 
  var SIGNATURE = '<a href="https://www.sopimat.com/" target="_blank"><img src="https://i.ibb.co/V9jjDkc/sopimat-signature-png-1.png" alt="Sopimat" border="0" class="center"></a>';
  
  
  
  try {
    var group = ContactsApp.getContactGroup(CONTACTGROUP);
    
    if (group) {
      
      var emailSUBJECT  = "Your contact information";    
      var Contacts = group.getContacts();
      
      for (i=0; i<Contacts.length; i++) {
        
        var email = Contacts[i].getPrimaryEmail();
        
        
        
        if (email && email.length) {
          
          var ID = Contacts[i].getId();
          ID = ID.substr(ID.lastIndexOf("/") + 1);
          
          var emailBody = "Hi,<br /><br />" +
            "We created a Form using Google Scripts to let our contacts update their own contact information on our Google Contacts database, in order to keep all the details updated and make sure that you receive all the necessary information from us.<br /><br />Please take a moment to update your contact information in my address book.<br /><br />" + 
              "To proceed, <a href='" + ScriptApp.getService().getUrl() + "?id=" + 
                ID + "'>click here</a> to open the form and fill-in your updated contact details. " +
                  "Your information will be automatically added to my Google Contacts." + "<br /><br />" + "<a href='" + ScriptApp.getService().getUrl() + "?id=" + 
                ID + "'><b>Go to Form ►►</b></a>" +
                    "<br /><br />Thanks,<br />" + NAME + "<br /><br />" + SIGNATURE;
          
          GmailApp.sendEmail(email, emailSUBJECT, emailBody, 
                             {htmlBody:emailBody, name:NAME});
        }
      }
    }   
  } catch (e) {
    throw e.toString();
  }
}

function doGet(e) {
  var html = HtmlService.createTemplateFromFile("form.html");
  html.id = e.parameter.id;
  var contact = GetBasicContact(e.parameter.id);
  html.email = contact.EMAIL;
  html.name = contact.NAME;
  return html.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

function GetBasicContact(id) {    
    
  var contact = {};
  
  contact.NAME = "";
  contact.EMAIL = "";

  id = "https://www.google.com/m8/feeds/contacts/" + encodeURIComponent(Session.getEffectiveUser().getEmail()) + "/base/" + id; 
  
  var c = ContactsApp.getContactById(id);    
  
  if (c) {
    
    if (c.getFullName().length)
      contact.NAME = c.getFullName();
    
    if(c.getEmails(ContactsApp.Field.HOME_EMAIL).length)
      contact.EMAIL = c.getEmails(ContactsApp.Field.HOME_EMAIL)[0].getAddress();
    
  }    
  
  return contact;
  
}

function GetContact(id) {    
    
  var contact = {};  
  
  contact.FOUND = 0;
  contact.id = id;
  
  try {
    
    var c = ContactsApp.getContactById(id);    
    
    if (c) {
            
      contact.FOUND = 1;
      
      if (c.getFullName().length)
        contact.FULL_NAME = c.getFullName();
      
      if(c.getEmails(ContactsApp.Field.HOME_EMAIL).length)
        contact.HOME_EMAIL = c.getEmails(ContactsApp.Field.HOME_EMAIL)[0].getAddress();

      if(c.getPhones(ContactsApp.Field.MOBILE_PHONE).length)
        contact.MOBILE_PHONE = c.getPhones(ContactsApp.Field.MOBILE_PHONE)[0].getPhoneNumber();
      
      if(c.getAddresses(ContactsApp.Field.HOME_ADDRESS).length) {
        contact.HOME_ADDRESS = c.getAddresses(ContactsApp.Field.HOME_ADDRESS)[0].getAddress();
        contact.HOME_ADDRESS = contact.HOME_ADDRESS.replace(/\n/g, ", ");
      }
      
      if(c.getIMs(ContactsApp.Field.SKYPE).length)
        contact.SKYPE = c.getIMs(ContactsApp.Field.SKYPE)[0].getAddress();
            
      if(c.getUrls(ContactsApp.Field.BLOG).length)
        contact.BLOG = c.getUrls(ContactsApp.Field.BLOG)[0].getAddress();
      
      if(c.getDates(ContactsApp.Field.BIRTHDAY).length) {
        var months = ["0", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", 
                      "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
        contact.BIRTHDAY = months.indexOf(c.getDates(ContactsApp.Field.BIRTHDAY)[0].getMonth().toString()) +
          "/" + c.getDates(ContactsApp.Field.BIRTHDAY)[0].getDay() +
          "/" + c.getDates(ContactsApp.Field.BIRTHDAY)[0].getYear();
      }      
    }    
    
    return contact;
  
  } catch (e) {
  
    return contact;    
  
  }  
  
}


function UpdateContact(contact) {    
  
  try {   
            
    var cid = "https://www.google.com/m8/feeds/contacts/" + encodeURIComponent(Session.getEffectiveUser().getEmail()) + "/base/" + contact.id; 
    
    var c = ContactsApp.getContactById(cid);    
    
    if (c) { 
                  
      c.setFullName(contact.FULL_NAME);
            
      if(c.getIMs(ContactsApp.Field.SKYPE).length)
        c.getIMs(ContactsApp.Field.SKYPE)[0].deleteIMField();
      
      if (contact.SKYPE.length)
        c.addIM(ContactsApp.Field.SKYPE, contact.SKYPE);   
      
      if (c.getAddresses(ContactsApp.Field.HOME_ADDRESS).length)
        c.getAddresses(ContactsApp.Field.HOME_ADDRESS)[0].deleteAddressField();
      
      if (contact.HOME_ADDRESS.length)
        c.addAddress(ContactsApp.Field.HOME_ADDRESS, contact.HOME_ADDRESS);
      
      if (c.getPhones(ContactsApp.Field.MOBILE_PHONE).length)
        c.getPhones(ContactsApp.Field.MOBILE_PHONE)[0].deletePhoneField();
      
      if (contact.MOBILE_PHONE.length)
        c.addPhone(ContactsApp.Field.MOBILE_PHONE, contact.MOBILE_PHONE);
      
      if (c.getUrls(ContactsApp.Field.BLOG).length)
        c.getUrls(ContactsApp.Field.BLOG)[0].deleteUrlField();
      
      if (contact.BLOG.length)
        c.addUrl(ContactsApp.Field.BLOG, contact.BLOG);     
      
      if(contact.TWITTER.length) { 
        var cfields = c.getCustomFields();
        for (var i = 0; i < cfields.length; i++) {
          if (cfields[i].getLabel() == 'Twitter') {
            cfields[i].deleteCustomField();
          }
        }
        c.addCustomField("Twitter", "https://twitter.com/" + contact.TWITTER);
      }
            
      if (contact.BIRTHDAY.length) {
        
        var months = 
            [ 0, ContactsApp.Month.JANUARY, ContactsApp.Month.FEBRUARY, ContactsApp.Month.MARCH,
             ContactsApp.Month.APRIL, ContactsApp.Month.MAY, ContactsApp.Month.JUNE,
             ContactsApp.Month.JULY, ContactsApp.Month.AUGUST, ContactsApp.Month.SEPTEMBER,
             ContactsApp.Month.OCTOBER, ContactsApp.Month.NOVEMBER, ContactsApp.Month.DECEMBER
            ];
        
        var date = contact.BIRTHDAY.split("/");
        
        if (c.getDates(ContactsApp.Field.BIRTHDAY).length)
          c.getDates(ContactsApp.Field.BIRTHDAY)[0].deleteDateField();
        
        c.addDate(ContactsApp.Field.BIRTHDAY, months[parseFloat(date[0])], parseFloat(date[1]), parseFloat(date[2]));
        
      }
      
      GmailApp.sendEmail(Session.getEffectiveUser().getEmail(),
                        "Updated: " + contact.FULL_NAME + " (" + contact.HOME_EMAIL + ")", 
                        Utilities.jsonStringify(contact));
      
      
    }    
    
  } catch (e) {
    
  }  
  
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">

    <llink rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <style>
    
@media screen and (max-width:980px) {
html, body, iframe#sandboxFrame, .full_size {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    background-color: #eeeeee;
}
body {
    color: #222;
/*    font-size: 2.3vw !important; */
/*    line-height:2.8vw !important; */
    background-color: #eeeeee;
}

.contacts { 
    width: 95%; 
    margin: auto;  
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    overflow-y: auto;
    padding: 12px;
    } 
    
    h2 {
    color: #000;
/*    font-size: 3.2vw !important; */
/*    line-height:4.2vw !important; */
    text-align: center;
    border-bottom: 1px #333 solid;
    padding-bottom: 15px;
/*    margin-bottom: 30px; */
}
    .control-label {
    display: flex;
    min-width: 29%!important;
    color: #222;
    font-size: 3vw !important;
    line-height:3vw !important;
    text-align: left;
    }
    
    input[type="date"], input[type="datetime"], input[type="datetime-local"], input[type="email"], input[type="month"], input[type="number"], input[type="password"], input[type="search"], input[type="tel"], input[type="text"], input[type="time"], input[type="url"], input[type="week"], select[multiple], select[size], textarea {
    background: #fff;
    border: 2px solid #9e9e9e;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #000;
    font-family: arial, sans-serif;
    font-size: 3vw !important;
    font-weight: normal;
    margin: auto;
    outline: 0;
    padding: 4px;
    width: 100%;
    float:none;
    height: 65px;
    text-align: left;
}

.button.action, .button.blue, button.action, button.blue, input[type="button"].action, input[type="button"].blue, input[type="submit"].action, input[type="submit"].blue {
    background: -moz-linear-gradient(top, #c42839, #c42839);
    background: -ms-linear-gradient(top, #c42839, #c42839);
    background: -o-linear-gradient(top, #c42839, #c42839);
    /* background-color: #c42839!important; */
    border: 1px solid #3079ed;
    color: #fff;
    margin-top: 15px;
    margin-bottom: 10px;
    margin-left: 35%;
    width: 30%;
    border-radius: 55px;
    font-family: arial, sans-serif;
    font-size: 3vw !important;
    font-weight: bold;
    height: 75px;
    line-height: 30px;
    }

.button, button, input[type="button"], input[type="image"], input[type="reset"], input[type="submit"] {
    background: -moz-linear-gradient(top, #c42839, #c42839);
    background: -ms-linear-gradient(top, #c42839, #c42839);
    background: -o-linear-gradient(top, #c42839, #c42839);
    background: -webkit-linear-gradient(top, #c42839, #c42839);
    background: linear-gradient(top, #c42839, #c42839);
    border: 1px solid #dcdcdc;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    color: #333;
    cursor: default;
    font-family: arial, sans-serif;
    font-size: 14px;
    font-weight: bold;
    height: 35px;
    line-height: 27px;
    margin: 0;
    min-width: 72px;
    outline: 0;
    padding: 0 8px;
    text-align: center;
    white-space: nowrap;    
}
#message {
    text-align: center;
    margin-top: -10px;
    font: 15px/20px arial, sans-serif !important;
    margin-bottom: 30px;
}
}

@media screen and (min-width:981px) {
html, body, iframe#sandboxFrame, .full_size {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    background-color: #eeeeee;
}
.contacts { 
    width: 560px; 
    margin: 0 auto;  
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    overflow-y: auto;
    padding: 12px;
    } 


    h2 {
    color: #000;
    font: 16px/24px arial, sans-serif normal;
    text-align: center;
    border-bottom: 1px #333 solid;
    padding-bottom: 15px;
    margin-bottom: 30px;
}
    .control-label {
    display: inline-block;
    min-width: 29%!important;
    color: #222;
    font-size: 15px !important;
    line-height:20px !important;
/*    font: 15px/20px arial, sans-serif; */
    text-align: left;
    }
    
    input[type="date"], input[type="datetime"], input[type="datetime-local"], input[type="email"], input[type="month"], input[type="number"], input[type="password"], input[type="search"], input[type="tel"], input[type="text"], input[type="time"], input[type="url"], input[type="week"], select[multiple], select[size], textarea {
    background: #fff;
    border: 1px solid #9e9e9e;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #000;
    font-family: arial, sans-serif;
    font-size: 13px;
    font-weight: normal;
    margin: 0;
    outline: 0;
    padding: 4px;
    width: 70%;
    float:right;
    text-align: left;
}

.button.action, .button.blue, button.action, button.blue, input[type="button"].action, input[type="button"].blue, input[type="submit"].action, input[type="submit"].blue {
    background: -moz-linear-gradient(top, #c42839, #c42839);
    background: -ms-linear-gradient(top, #c42839, #c42839);
    background: -o-linear-gradient(top, #c42839, #c42839);
    /* background-color: #c42839!important; */
    border: 1px solid #3079ed;
    color: #fff;
    margin-top: 25px;
    margin-bottom: 20px;
    margin-left: 37%;
    width: 26%;
    border-radius: 55px;
    font-family: arial, sans-serif;
    font-size: 14px;
    font-weight: bold;
    height: 35px;
    line-height: 27px;
}
.button, button, input[type="button"], input[type="image"], input[type="reset"], input[type="submit"] {
    background: -moz-linear-gradient(top, #c42839, #c42839);
    background: -ms-linear-gradient(top, #c42839, #c42839);
    background: -o-linear-gradient(top, #c42839, #c42839);
    background: -webkit-linear-gradient(top, #c42839, #c42839);
    background: linear-gradient(top, #c42839, #c42839);
    border: 1px solid #dcdcdc;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    color: #333;
    cursor: default;
    font-family: arial, sans-serif;
    font-size: 14px;
    font-weight: bold;
    height: 35px;
    line-height: 27px;
    margin: 0;
    min-width: 72px;
    outline: 0;
    padding: 0 8px;
    text-align: center;
    white-space: nowrap;
}
#message {
    text-align: center;
    margin-top: -20px;
    font: 15px/18px arial, sans-serif;
    margin-bottom: 20px;
}
}
.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  filter: brightness(0.94);
  margin-bottom: 50px;
}
.center_text{
    text-align: center;
}
    </style>

<div class="contacts">
  <h2 class="title">Google Contacts Updater</h2>
  <p id="message">
    Please, fill the form below to update your contact information.<br>Thanks for your cooperation! 
  </p>

  <form id="contactform">
    <p>
      <label class="control-label" for="inputName">Your Name</label>
      <input type="text" id="inputFULL_NAME" placeholder="Full Name" value="<?= name ?>">
    </p>

    <p>
      <label class="control-label" for="inputEmail">Email Address</label>
      <input type="text" id="inputHOME_EMAIL" class="input-large" placeholder="i.e.: name@yourdomain.com">
    </p>
    
    <p>  
      <label class="control-label" for="inputMobilePhone">Mobile Phone</label>
      <input type="text" id="inputMOBILE_PHONE" class="input-medium" placeholder="">
    </p>
    
    <p>
      <label class="control-label" for="inputAddressWork">Street Address</label>
      <input type="text" id="inputHOME_ADDRESS" class="input-xlarge" placeholder="">
    </p>

    <p>
      <label class="control-label" for="inputSkype">Skype</label>
      <input type="text" id="inputSKYPE" class="input-small"  placeholder="@username">
    </p>

    <p><label class="control-label" for="inputTwitter">Twitter</label>
      <input type="text" id="inputTWITTER" class="input-small" placeholder="@username">
    </p>  
    
    <p>
      <label class="control-label" for="inputDOB">Birthday</label>
      <input type="text" id="inputBIRTHDAY" class="input-small" placeholder="MM/DD/YYYY">
    </p>

    <p>
      <label class="control-label" for="inputBlog">Website</label>
      <input type="text" id="inputBLOG" class="input" placeholder="i.e.: yourwebsite.com">
    </p>
    <p>
      <input type="hidden" name="id" value="<?= id; ?>" />
      <input type="submit" class="btn blue" value="Update Contact" onclick="updateGoogleContacts(); return false;">
    </p>
  </form>
  <hr />
  <p class="center_text">If you have any questions send an email to <a href="mailto:info@testdomain.com?Subject=Google%20Contacts%20Updater%20Script">info@testdomain.com</a></p>

</div>



<script>

  function progress() {
    $("#message").html("Your contact details have been updated. Thanks!");
    return;
  }

  function updateGoogleContacts() {

    var contact = {};

    contact.FULL_NAME    = $("input#inputFULL_NAME").val();
    contact.HOME_EMAIL   = $("input#inputHOME_EMAIL").val();
    contact.HOME_ADDRESS = $("input#inputHOME_ADDRESS").val();
    contact.MOBILE_PHONE = $("input#inputMOBILE_PHONE").val();
    contact.SKYPE        = $("input#inputSKYPE").val();
    contact.TWITTER      = $("input#inputTWITTER").val();
    contact.BLOG         = $("input#inputBLOG").val();
    contact.BIRTHDAY     = $("input#inputBIRTHDAY").val();
    contact.id           = "<?= id ?>";

    google.script.run.withSuccessHandler(progress).UpdateContact(contact);      

    $("#contactform").hide();
    $("#message").html("Updating your contact...");

    return false;

  }

</script>

标签: javascriptgoogle-apps-scriptgmail-apigoogle-contacts-api

解决方案


推荐阅读