Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

email - Sending mail error with python smtplib

I am attempting to use the python 3.2 SMTPlib.sendmail() function to send a message, after some modifcation of the SMTP library (namely commenting out the rset() function which was suppressing the error msg) I managed to retrieve the following error message from the server:

SendMail Failed (554, b'Transaction failed : Cannot send message due to possible abuse; please visit http://postmaster.yahoo.com/abuse_smtp.html for more information')

The yahoo mail SMTP server thinks I'm sending spam, the URL does link to anything useful. I think it has to do with an inadequate header, I can't seem to find a definitive answer on what constitutes a compliant header & I've read of simmilar issues with Gmail. Mock emails have been substituted for this post.

Any help would be appreciated

My full code is below:

    self.message =  email.message_from_string('''To: <[email protected]>
    From: <[email protected]>
    Reply-To: <[email protected]>
    Subject: Test send mail 

 Hello''')
    fromAddress = '[email protected]'
    toAddress = '[email protected]'
    try:
        self.smtp = SMTP()
        self.smtp.connect('smtp.mail.yahoo.com')
    except Exception:
        print('Connection Failed')
        print(traceback.format_exc())
    try:
        self.smtp.login('rwilson','tree22')
    except Exception:
        print('Login Failed!')
        print(traceback.format_exc())
    try:
        self.smtp.sendmail(fromAddress,toAddress ,self.message.as_string())
        print("Message sucessfully sent!")
        self.smtp.close()
    except Exception as e:
        print('SendMail Failed')
        print(e)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following works for microsoft, google, yahoo accounts on Python 2.7 and Python 3.2:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Send email via smtp_host."""
import smtplib
from email.mime.text import MIMEText
from email.header    import Header

####smtp_host = 'smtp.live.com'        # microsoft
####smtp_host = 'smtp.gmail.com'       # google
smtp_host = 'smtp.mail.yahoo.com'  # yahoo
login, password = ...
recipients_emails = [login]

msg = MIMEText('body…', 'plain', 'utf-8')
msg['Subject'] = Header('subject…', 'utf-8')
msg['From'] = login
msg['To'] = ", ".join(recipients_emails)

s = smtplib.SMTP(smtp_host, 587, timeout=10)
s.set_debuglevel(1)
try:
    s.starttls()
    s.login(login, password)
    s.sendmail(msg['From'], recipients_emails, msg.as_string())
finally:
    s.quit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...