使用Python调用Gmail API发送邮件被标记为垃圾/钓鱼邮件且缺少签名的问题求助
使用Python调用Gmail API发送邮件被标记为垃圾/钓鱼邮件且缺少签名的问题求助
我刚学会用Python脚本调用Gmail API自动发送邮件,目前功能正常,但遇到两个棘手的问题:
- 用这种方式发送的邮件都会被标记为「可能是垃圾邮件或钓鱼邮件」,导致部分服务器可能直接拦截它们
- 发送的邮件里没有显示我的邮箱签名
我猜测这两个问题应该是相关的,可能是因为API调用没有走完整的账号身份验证流程,没能正确关联到我的主邮箱,但我试了好几种方法都没能解决。
下面是我实际使用的代码:
import os import pickle #Gmail API utils from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request # for encoding/decoding messages in base64 from base64 import urlsafe_b64decode, urlsafe_b64encode # for dealing with attachment MIME types from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from email.mime.audio import MIMEAudio from email.mime.base import MIMEBase from mimetypes import guess_type as guess_mime_type # Request all access (permission to read/send/receive emails, manage the inbox, and more) SCOPES = ['https://mail.google.com/'] my_email = 'sample@gmail.com' def gmail_authenticate(): 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) return build('gmail', 'v1', credentials=creds) # get the Gmail API service service = gmail_authenticate() # Adds the attachment with the given filename to the given message def add_attachment(message, filename): content_type, encoding = guess_mime_type(filename) if content_type is None or encoding is not None: content_type = 'application/octet-stream' main_type, sub_type = content_type.split('/', 1) if main_type == 'text': fp = open(filename, 'rb') msg = MIMEText(fp.read().decode(), _subtype=sub_type) fp.close() elif main_type == 'image': fp = open(filename, 'rb') msg = MIMEImage(fp.read(), _subtype=sub_type) fp.close() elif main_type == 'audio': fp = open(filename, 'rb') msg = MIMEAudio(fp.read(), _subtype=sub_type) fp.close() else: fp = open(filename, 'rb') msg = MIMEBase(main_type, sub_type) msg.set_payload(fp.read()) fp.close() filename = os.path.basename(filename) msg.add_header('Content-Disposition', 'attachment', filename=filename) message.attach(msg) def build_message(destination, obj, body, attachments=[]): if not attachments: # no attachments given message = MIMEText(body) message['to'] = destination message['from'] = my_email message['subject'] = obj else: message = MIMEMultipart() message['to'] = destination message['from'] = my_email message['subject'] = obj message.attach(MIMEText(body)) for filename in attachments: add_attachment(message, filename) return {'raw': urlsafe_b64encode(message.as_bytes()).decode()} def send_message(serv, destination, obj, body, attachments=[]): return serv.users().messages().send( userId="me", body=build_message(destination, obj, body, attachments) ).execute() send_message(service, my_email, "Test subject", "This is the body of the e-mail")
有没有大佬能帮忙看看怎么解决这两个问题?非常感谢!
备注:内容来源于stack exchange,提问作者Flavio Fernandes
相关产品推荐
相关产品推荐

