Error while sending PDF as attachment using SMTP
02:44 31 Jan 2017

While running the below piece of python script to send pdf attachment using SMTP, encountered SMTPDataError exception. I am able to send text or image files using the below code with the same sender and to the same recipient. The pdf file size is hardly 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

This is HTML message.

This is headline.

""",'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()

The error message encountered

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')

Any help is much appreciated.

python pdf mime smtplib