在运行下面的python脚本以使用SMTP发送pdf附件时,遇到了SMTPDataError异常。我能够使用以下代码发送文本或图像文件与相同的发件人和相同的收件人。 pdf文件大小几乎不是1 MB。
import smtplib
import email
import email.mime
import email.mime.application
from_email = "XXXX@gmail.com"
from_passwd = ""
to_email = "abc@gmail.com"
message = email.mime.Multipart.MIMEMultipart('mixed')
message['Subject'] = 'Test_run'
message['From'] = from_email
message['To'] = to_email
text_part = email.mime.Text.MIMEText("""This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
""",'html')
message.attach(text_part)
filename1 = "some_doc.pdf"
fp = open(filename1 , 'rb')
attach_part = email.mime.application.MIMEApplication(fp.read(),"pdf")
fp.close()
attach_part.add_header('Content-Disposition','attachment',filename = "some_doc.pdf")
message.attach(attach_part)
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(from_email,from_passwd)
server.sendmail(from_email,to_email,message.as_string())
server.close()
遇到错误消息
File "/usr/lib/python2.7/smtplib.py", line 746, in sendmail
raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (550, '5.7.1 The user or domain that you are sending to (or from) has a policy that\n5.7.1 prohibited the mail that you sent. Please contact your domain\n5.7.1 administrator for further details. For more information, please visit\n5.7.1 https://support.google.com/a/answer/172179 66sm37804549pfx.29 - gsmtp')
非常感谢任何帮助。
答案 0 :(得分:0)
您是否尝试过将PDF发送给其他域中的其他收件人?或者将同样大小的图像发送给同一个收件人?
听起来这个域可能不允许pdf附件,无论出于何种原因......