web-dev-qa-db-ja.com

「安全でないアクセス」を有効にせずにGmail経由でメールを送信する方法

Googleは、Gmail SMTPサーバーへのスクリプトアクセスのセキュリティを改善することを求めています。私はそれで問題ありません。実際、私は喜んで手伝います。

しかし、彼らはそれを簡単にしていません。 Upgrade to a more secure app that uses the most up to date security measures、しかし、それは私がこのように見えるコードのビットをアップグレードする方法を解決するのに役立ちません:

server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(GMAIL_USER, GMAIL_PASSWORD)
server.sendmail(FROM, TO, MESSAGE)
server.close()

確かに、「安全性の低いアプリへのアクセス」を有効にしますが、誰かがこのコードを置き換えるものを考え出したなら、感謝します。

25
John Mee

ジョン・ミーの答えは時代遅れのようです。 2016年7月には機能しません。GmailのAPIの更新が原因である可能性があります。以下のように彼のコード(python 2)を更新します。

    """Send an email message from the user's account.
"""

import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os

#from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

from apiclient import errors

SCOPES = 'https://www.googleapis.com/auth/gmail.compose'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

def SendMessage(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
    message = (service.users().messages().send(userId=user_id, body=message)
               .execute())
    print 'Message Id: %s' % message['id']
    return message
  except errors.HttpError, error:
    print 'An error occurred: %s' % error


def CreateMessage(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.urlsafe_b64encode(message.as_string())}


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sendEmail.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

if __name__ == "__main__":

    try:
        credentials = get_credentials()
        http = credentials.authorize(httplib2.Http())
        service = discovery.build('gmail', 'v1', http=http)
        SendMessage(service, "me", CreateMessage("[email protected]", "[email protected]", "Test gmail automation", "Hello world"))

    except Exception, e:
        print e
        raise

エラーが発生した場合はInsufficient Permission、考えられる理由の1つは、プログラムのscopeが正しく設定されていないことです。他の考えられる理由は、ストレージjsonファイル(このプログラムでは "sendEmail.json")を削除し、プログラムを更新する必要がある可能性があります。詳細については、この post を参照してください。

16
ccy

これはつらいことでしたが、今は何かが起こっているようです...

Python3はサポートされていません(まだ)

私が達成するのはそれほど難しいとは思わない。なぜなら、私はパッケージを変換するのをつまずいていたので、大したことは何もしなかった。普通の2to3のものだけだ。しかし、数時間後、私は上流で泳ぐのに疲れました。この記事を書いている時点では、Python 3の一般消費向けの公開パッケージを見つけることができませんでした。python 2の経験は簡単でした(比較)。

Googleウェブサイトの操作は戦いの半分です

間違いなく、時間が経つにつれて、これは変わるでしょう。最終的には、client_secret.jsonファイルをダウンロードする必要があります。あなたは(おそらく)ウェブブラウザ経由でのみこの設定を行うことができます:

  1. GoogleアカウントまたはGoogleアプリまたはGmailが必要です。だから、もし持っていないなら、持って行ってください。
  2. 開発者コンソール に自分自身を取得します
  3. 新しいプロジェクトを作成し、それが完了するまで4〜400秒待ちます。
  4. API's and Auth-> Credentialsに移動します
  5. OAuthの下でCreate New Client IDを選択します
  6. アプリケーションの種類としてInstalled Applicationを選択し、Otherを選択します
  7. ボタンDownload JSONが必要です。それを行う。それはあなたのclient_secret.json—いわばパスワードです

しかし、それだけではありません!

奇妙なエラーを避けるために、アプリケーションに「製品名」を指定する必要があります。 (これをあなたに与えるために私がどれだけ苦しんだか見てください;-)

  1. API's & auth-> Consent Screenに移動します
  2. メールを選択してください
  3. 製品名を入力してください。それが何であるかは関係ありません。 「Foobar」で問題ありません。
  4. セーブ

ニュースフラッシュ!うわあ。さらに多くの機能が追加されました!

  1. APIと認証-> API-> Gmail APIに移動します
  2. [APIを有効にする]ボタンをクリックします

わーい。これで、メール送信スクリプトを更新できます。

Python 2

初めて対話的にスクリプトを実行する必要があります。それはあなたのマシンでウェブブラウザを開き、許可を与えます(ボタンを押します)。この演習では、コンピューターにファイルを保存しますgmail.storage再利用可能なトークンを含みます。

[グラフィカルブラウザ機能のないマシンにトークンを転送することはできませんでした。HTTPErrorを返します。 lynxグラフィカルブラウザー経由でそれを通過しようとしました。また、グーグルが最後の「承認」ボタンを「無効」に設定しているため失敗しました!?このハードルを克服するために別の質問をします(より不平を言う)]

まず、いくつかのライブラリが必要です。

pip install --upgrade google-api-python-client
pip install --upgrade python-gflags
  • 宛先アドレスと送信元アドレスを変更する必要があります
  • Storage命令が期待するところにclient_token.jsonファイルがあることを確認してください
  • gmail.storageファイルを保存できるように、ディレクトリは書き込み可能である必要があります

最後にいくつかのコード:

import base64
import httplib2

from email.mime.text import MIMEText

from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run


# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'

# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.compose'

# Location of the credentials storage file
STORAGE = Storage('gmail.storage')

# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, STORAGE, http=http)

# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)

# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)

# create a message to send
message = MIMEText("Message goes here.")
message['to'] = "[email protected]"
message['from'] = "[email protected]"
message['subject'] = "your subject goes here"
body = {'raw': base64.b64encode(message.as_string())}

# send it
try:
  message = (gmail_service.users().messages().send(userId="me", body=body).execute())
  print('Message Id: %s' % message['id'])
  print(message)
except Exception as error:
  print('An error occurred: %s' % error)

うまくいけば、私たち全員が始められます。古い方法ほど単純ではありませんが、今ではかなり複雑に見えませんが、肉体で見ることができます。

30
John Mee

Gmail APIの使用を検討しましたか? APIにはセキュリティ機能が組み込まれており、Gmail専用に最適化されています。 APIドキュメントは http://developers.google.com にあります。たとえば、Send API呼び出しのドキュメントは次のとおりです。

https://developers.google.com/gmail/api/v1/reference/users/messages/send

5
Amber

python 3使用のために更新されたいくつかのコードを含めています-必要な許可とOAuthトークンが機能している場合、メールを送信するようです。主にGoogle API Webサイトのサンプルに基づいています

    from __future__ import print_function

import base64
import os
from email.mime.text import MIMEText

import httplib2
from apiclient import discovery
from googleapiclient import errors
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
    import argparse

    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/gmail-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/gmail.send'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'gmail-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


to = '[email protected]'
sender = '[email protected]'
subject = 'test emails'
message_text = 'hello this is a text test message'
user_id = 'me'

def create_message(sender, to, subject, message_text):
    """Create a message for an email.

    Args:
      sender: Email address of the sender.
      to: Email address of the receiver.
      subject: The subject of the email message.
      message_text: The text of the email message.

    Returns:
      An object containing a base64url encoded email object.
    """
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': (base64.urlsafe_b64encode(message.as_bytes()).decode())}


def send_message(service, user_id, message):
    """Send an email message.

    Args:
      service: Authorized Gmail API service instance.
      user_id: User's email address. The special value "me"
      can be used to indicate the authenticated user.
      message: Message to be sent.

    Returns:
      Sent Message.
    """
    try:
        message = (service.users().messages().send(userId=user_id, body=message)
                   .execute())
        print('Message Id: {}'.format(message['id']))
        return message
    except errors.HttpError as error:
        print('An error occurred: {}'.format(error))


def main():
    """Shows basic usage of the Gmail API.

    Creates a Gmail API service object and outputs a list of label names
    of the user's Gmail account.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)

    msg = create_message(sender,to,subject,message_text)
    message = (service.users().messages().send(userId=user_id, body=msg)
               .execute())
    print('Message Id: {}'.format(message['id']))
    results = service.users().messages().list(userId='me').execute()
    labels = results.get('labels', [])

    if not labels:
        print('No labels found.')
    else:
        print('Labels:')
        for label in labels:
            print(label['name'])


if __name__ == '__main__':
    main()
4
jjisnow

Python 3、GMailの現在のAPI、以下のサンプルを更新しました。

credentials.jsonファイルの下に、関連するGCPプロジェクトを選択した後、OauthクライアントIDクレデンシャル ここ を作成する必要があります。作成したら、クライアントキーとクライアントシークレットが表示されます。そのプロンプトを閉じて、アカウントの横にある下矢印をクリックします。これが必要なファイルです。

enter image description here

import base64
import logging
import mimetypes
import os
import os.path
import pickle
from email.mime.text import MIMEText
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient import errors
from googleapiclient.discovery import build

def get_service():
    """Gets an authorized Gmail API service instance.

    Returns:
        An authorized Gmail API service instance..
    """    

    # If modifying these scopes, delete the file token.pickle.
    SCOPES = [
        'https://www.googleapis.com/auth/gmail.readonly',
        'https://www.googleapis.com/auth/gmail.send',
    ]

    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gmail', 'v1', credentials=creds)
    return service

def send_message(service, sender, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
    sent_message = (service.users().messages().send(userId=sender, body=message)
               .execute())
    logging.info('Message Id: %s', sent_message['id'])
    return sent_message
  except errors.HttpError as error:
    logging.error('An HTTP error occurred: %s', error)

def create_message(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  s = message.as_string()
  b = base64.urlsafe_b64encode(s.encode('utf-8'))
  return {'raw': b.decode('utf-8')}

if __name__ == '__main__':
    logging.basicConfig(
        format="[%(levelname)s] %(message)s",
        level=logging.INFO
    )

    try:
        service = get_service()
        message = create_message("[email protected]", "[email protected]", "Test subject", "Test body")
        send_message(service, "[email protected]", message)

    except Exception as e:
        logging.error(e)
        raise
0
Steve Gore