我执行代码时遇到以下错误:
An error occurred: <HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Insufficient Permission">
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import httplib2
import os
from httplib2 import Http
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
#SCOPES = 'https://www.googleapis.com/'
SCOPES = 'https://www.googleapis.com/auth/gmail.compose'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid, …Run Code Online (Sandbox Code Playgroud) 我正在将Gmail API合并到我正在制作的程序中,我收到的错误是我无法解决/我无法在线找到答案.相关代码如下,以及错误:
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
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
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):
message = (service.users().messages().send(userId=user_id, body=message).execute())
print('Message Id: %s' % message['id'])
return message
def send_email(orders):
SCOPES …Run Code Online (Sandbox Code Playgroud) 我正在尝试发送带有附件(最好是多个附件)的电子邮件,这些附件大于10 MB并且小于25 MB的总限制。我提到10 MB的原因是,这似乎是正常的附加文件方式停止起作用时得到的下限Error 10053。
我在文档中已经读到,做到这一点的最佳方法是使用可恢复的上载方法,但我无法使其正常工作,也无法在Python中找到任何好的示例。关于此的大多数SO问题都只是链接回到没有Python示例的文档,否则它们的代码会导致其他错误。
我正在寻找Python中的解释,因为我想确保自己理解正确。
我浏览过的问题:
码:
import base64
import json
import os
from email import utils, encoders
from email.message import EmailMessage
from email.mime import application, multipart, text, base, image, audio
import mimetypes
from apiclient import errors
from googleapiclient import discovery, http
from google.oauth2 import service_account
def send_email(email_subject, email_body, email_sender='my_service_account@gmail.com', email_to='', email_cc='', email_bcc='', files=None):
# Getting credentials
with open(os.environ.get('SERVICE_KEY_PASSWORD')) as f:
service_account_info …Run Code Online (Sandbox Code Playgroud) python mime email-attachments google-api-python-client gmail-api
我有一个 Python 脚本可以做到这一点。我必须在 Gmail 帐户中启用某些功能。大概三年来,脚本都是这样运行的:
import smtplib, ssl
...
subject = 'some subject message'
body = """text body of the email"""
sender_email = 'my_gmail_account_name@gmail.com'
receiver_email = 'some_recipient@something.com'
# Create a multipart message and set headers
message = MIMEMultipart()
message['From'] = 'Mike'
message['To'] = receiver_email
message['Subject'] = subject
# Add body to email
message.attach(MIMEText(body, 'plain'))
# Open file in binary mode
with open( client_zip_filename, 'rb') as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as …Run Code Online (Sandbox Code Playgroud) 我想使用Gmail API通过Python发送电子邮件.Everythings应该没问题,但我仍然得到错误"发生错误:b'Q29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJ1cy1hc2NpaSIKTUlNRS ..."这是我的代码:
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_flow
# 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 …Run Code Online (Sandbox Code Playgroud) 我试图用smtp模块发送电子邮件,但我有一个错误:
File "/usr/lib/python2.7/smtplib.py", in login
raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, '5.7.14)...
Run Code Online (Sandbox Code Playgroud)
有人已经有这个错误吗?你知道怎么解决吗?
代码:
def sendNotification():
recepients_list = "emailsmtplibtest@gmail.com"
subject = 'Subject'
message = "Message"
sendemail(recepients_list,subject,message)
def sendemail(to_addr_list, subject, message):
username = 'emailsmtplibtest@gmail.com'
password = 'passtest'
from_addr = 'emailsmtplibtest@gmail.com'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username,password)
newmessage = '\r\n'.join([
'To: %s' %recepient_list,
'From: %s' % from_addr,
'Subject: %s' %subject,
'',
message
])
try:
server.sendemail(from_addr, to_addr_list,newmessage)
print 'notification sent'
except:
print 'error sending notification'
server.quit()
sendNotification()
Run Code Online (Sandbox Code Playgroud) 我试图让python程序通过Gmail发送附件。我使用发现的示例代码: 通过gmail和python发送电子邮件
问题是,当我向自己发送.xls,.xlsx,.xlsm之类的Excel文件时,即使原始文件没问题,我也无法打开附件,因为它们已损坏。但是发送.csv可以正常工作。整个过程不会弹出任何警告或错误。
问题是:oauth2.0或Gmail或MIME是否将某些格式的附件弄乱了?以及如何在不修改程序的情况下告诉程序上传和发送附件?
该python 3脚本假设用于创建电子邮件,将单个文件(使用其url)附加到该电子邮件并发送。它发送了电子邮件,但是create_message_with_attachment()
TypeError:附加内容在非分段有效载荷的邮件上无效
我确实阅读了Google文档。谈论它的堆栈线程专注于精美的附件样式,同时在其顶部混合了python版本的不同语法。
下面的代码是几个来源的拼凑而成。我努力使他们一起参加create_message_with_attachment()。
例如,我不知道是否应包含此代码(来自于此代码的create_message_without_attachment()。底部的Cf)
raw = base64.urlsafe_b64encode(msg.as_bytes())
raw = raw.decode()
body = {'raw': raw}
return body
Run Code Online (Sandbox Code Playgroud)
带有附件代码的创建消息:
import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email import encoders
#needed for attachment
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#needed for gmail service
from apiclient import errors, discovery
#The scope URL for read/write access to the gmail api
SCOPES = 'https://www.googleapis.com/auth/gmail.send'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python …Run Code Online (Sandbox Code Playgroud)