通过cron作业发送多封电子邮件时,是否有可能通过Gmail阻止电子邮件?

时间:2018-04-10 10:15:11

标签: python cron gmail-api

我对我的代码行为有疑问。以下代码执行以下操作: - 获取gmail API的凭据 - 尝试发送消息 - 与此同时,一项cron工作正在进行中

我注意到他们一起工作得很好。现在,第一条消息发送和接收良好。但随后发送了第二封和所有其他邮件,但从未到达收据。收件箱中收到第一封电子邮件。我检查了垃圾邮件文件夹,但那里什么也没有。我检查了发件人的已发送文件夹,邮件都已发送。 我是这两个元素的新手,我无法弄清楚发生了什么。

这是我的代码:

from __future__ import print_function
import httplib2
import os
import atexit
import time

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from flask import Flask

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger

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

app = Flask(__name__)

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 = 'http://mail.google.com'
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.
"""
current_dir = os.getcwd()
credential_dir = os.path.join(current_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


def create_message(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': base64.urlsafe_b64encode(message.as_string())}


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


@app.route("/home")
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)

message = create_message('foo@foo.com', 'foo@foo.com', 'Test', 'Ciao')
send_message(service, 'me', message)
print(time.strftime("%A, %d. %B %Y %I:%M:%S %p"))


scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(
    func=main,
    trigger=IntervalTrigger(seconds=20),
    id='printing_job',
    name='Print date and time every five seconds',
    replace_existing=True)
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())


if __name__ == '__main__':
    app.run()

我想知道我是否遗漏了什么。

我尝试发送收件人和发件人电子邮件地址,但一切都没有改变。

0 个答案:

没有答案