아래 코드는 작동합니다. Gmail API를 통해 이메일을 보낼 수는 있지만 벽 시간 (20-30 초)에 따라 10 개의 맞춤 이메일을 보내려면 잠시 시간이 걸립니다. 전자 메일을 더 빨리 보내도록 아래 코드를 최적화하는 방법이 있습니까? 할당량 제한이 있습니다.Gmail API Python : 이메일을 더 빨리 보내도록 코드 최적화
하루에 최대 100 개의 이메일을 보낼 수 있습니까? 발송할 수있는 이메일 수와 이메일 당 수령 수 사이에 차이가있는 것으로 보입니다. 소싱 할 문서입니다. https://developers.google.com/apps-script/guides/services/quotas
저는 소비자 버전을 사용하고 있습니다.
import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from apiclient import errors, discovery
import mimetypes
import pandas as pd
import textwrap
SCOPES = 'https://www.googleapis.com/auth/gmail.send'
CLIENT_SECRET_FILE = 'secret.json'
APPLICATION_NAME = 'AppName'
def get_credentials():
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-send.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
credentials = tools.run_flow(flow, store)
print 'Storing credentials to ' + credential_path
return credentials
def SendMessage(sender, to, subject,message_text):
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
message1 = CreateMessageHtml(sender, to, subject, message_text)
result = SendMessageInternal(service, "me", message1)
return result
def CreateMessageHtml(sender, to, subject, message_text):
msg = MIMEText(message_text)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to
return {'raw': base64.urlsafe_b64encode(msg.as_string())}
def SendMessageInternal(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 errors.HttpError, error:
print 'An error occurred: %s' % error
return "Error"
return "OK"
def main():
df = pd.read_csv('testdata.csv')
for index,row in df.iterrows():
to = row['Email']
sender = "sender"
subject = "subject"
dedent_text = '''Hello {}, \n
thank you for your question.'''.format(row['First'])
message_text = textwrap.dedent(dedent_text).strip()
SendMessage(sender, to, subject, message_text)
if __name__ == '__main__':
main()
:
기능 소비자 (gmail.com)
캘린더 이벤트는 5000
연락처
문서 2백50일 100 *
코드 당
이메일 수신자를 만들어 1,000 등재
이제 훨씬 빠르게 실행됩니다. 감사합니다. 나는 이것을 cmd 행에서 실행하고있다. 큰 배치마다 하나의 파이썬 호출을 할 때, pd.read_csv ('testdata.csv')에 필요한 모든 데이터를 가지고 main()을 한 번 호출해야한다는 것을 의미합니까? 모듈을 가져 오면 캐시 된 것 같습니다. – Jahmul14
명령 줄에서 파이썬 인터프리터를 점심을 먹을 때마다 모든 모듈이 다시로드된다는 것을 의미합니다. 전자 메일 일괄 처리를 빠르게 실행하려면 CSV 파일에 더 큰 일괄 처리가 포함되어있어 적은 수의 CSV 파일로 프로세스를 다시 실행해야하는 것이 좋습니다. – tennessee