【问题标题】:How to use gapi for sending email in Angular 9如何在 Angular 9 中使用 gapi 发送电子邮件
【发布时间】:2020-09-16 13:24:12
【问题描述】:

我在 angular9 中创建了一次服务并调用 google api 发送电子邮件,但它不起作用。有人可以帮助我该怎么办吗?我尝试了许多其他方法,但没有一种方法奏效。

sendEmail() {
    const scopes = [
      'https://www.googleapis.com/auth/gmail.send',
    ].join(' ');

    const message =
      "From: sample@gmail.com\r\n" +
      "To: sample2@gmail.com\r\n" +
      "Subject: As basic as it gets\r\n\r\n" +
      "This is the plain text body of the message.  Note the blank line between the header information and the body of the message.";

    const encodedMessage = btoa(message)

    const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

    gapi.load('client:auth2', () => {
      gapi.client.load('gmail', 'v1', () => {
        console.log('Loaded Gmail');
        gapi.client.init({
          apiKey: this.apiKey,
          discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'],
          client_id: this.clientId,
          immediate: true,
          scope: scopes
        }).then(res => {
          console.log('pop')
          return gapi.client.gmail.users.messages.send({
            userId: 'me',
            requestBody: {
              raw: reallyEncodedMessage
            }
          }).then(res => {
            console.log("done!", res)
          });
        })
      });
    });
  }

gapi 的回应:

kind: "discovery#restDescription"
id: "gmail:v1"
name: "gmail"
version: "v1"
rootUrl: "https://www.googleapis.com/"
servicePath: "gmail/v1/users/"
batchPath: "batch/gmail/v1"

...

【问题讨论】:

    标签: javascript angular gmail-api angular9 google-api-js-client


    【解决方案1】:

    我终于成功了,下面是我的代码:

      checkForGmailLogin() {
        console.log('onLoadCallbackFunction');
        const scope = ['https://mail.google.com/',
          'https://www.googleapis.com/auth/gmail.modify',
          'https://www.googleapis.com/auth/gmail.compose',
          'https://www.googleapis.com/auth/gmail.send'].join(' ');
        gapi.auth.authorize(
          {
            'client_id': this.clientId,
            'scope': scope,
            'immediate': true,
            discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest'],
          }, authResult => {
            if (authResult && !authResult.error) {
              gapi.client.load('gmail', 'v1', () => this.sendEmail());
            } else {
              console.log('Error in Load gmail');
            }
          });
      }
    
      sendEmail() {
        const subject = 'Code';
        const code = '123'
        const mimeData = [
          "From: from@gmail.com",
          "To: to@gmail.com",
          "To: to@yahoo.com",
          "Subject: =?utf-8?B?" + window.btoa(unescape(encodeURIComponent(subject))) + "?=",
          "MIME-Version: 1.0",
          "Content-Type: text/plain; charset=UTF-8",
          "Content-Transfer-Encoding: 7bit",
          "",
          "Your code is " + code].join("\n").trim();
        const raw = window.btoa(unescape(encodeURIComponent(mimeData))).replace(/\+/g, '-').replace(/\//g, '_');
        gapi.client.gmail.users.messages.send({
          'userId': 'me',
          'resource': {
            'raw': raw
          }
        }).execute(res => {
          console.log('Email sent', res);
          this.snackBar.success('Email has send Successfully')});
      }
    

    【讨论】:

      【解决方案2】:

      按照JavaScript example on the Gmail api reference,您必须更改您的请求:

      发件人:

      gapi.client.gmail.users.messages.send({
          userId: 'me',
          requestBody: {
              raw: reallyEncodedMessage
          }
      })
      

      收件人:

      gapi.client.gmail.users.messages.send({
          'userId': userId,
          'resource': {
            'raw': base64EncodedEmail
          }
        })
      
      

      如果这不能解决问题,请评论答案或提供有关问题的更多信息。

      【讨论】:

      • 感谢 Kessy,解决了。我怎么能联系到你,请把你的电子邮件发到 sadeghian.me@gmail.com
      猜你喜欢
      • 2020-08-21
      • 2021-06-25
      • 2022-07-26
      • 2019-07-17
      • 2018-07-14
      • 2019-02-03
      • 2019-09-25
      • 2018-08-24
      • 1970-01-01
      相关资源
      最近更新 更多