6

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: <ksmith@yahoo.co.nz>
    From: <rwilson@yahoo.co.nz>
    Reply-To: <rwilson@yahoo.co.nz>
    Subject: Test send mail \n\n Hello''')
    fromAddress = 'rwilson@yahoo.co.nz'
    toAddress = 'ksmith@yahoo.co.nz'
    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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Theodore Dunn
  • 61
  • 1
  • 1
  • 2
  • I would suggest to debug the issue using plain `telnet` sessions: Open a telnet connection to port 25 of the SMTP server, and try to deliver a mail by typing in the SMTP conversation manually. If that doesn't work either, your issue doesn't have anything to do with Python's SMTPlib, and you should continue trying to figure out why the SMTP server rejects your mail as spam. If it does work, there's an issue with your code. Use a packet sniffer like `tcpdump` and/or Wireshark, and compare you plain SMTP session with the SMTP conversation caused by your python code to figure out the difference. – Lukas Graf Oct 26 '13 at 14:52
  • 1
    I have given up trying to use yahoo, the code above seems to work ok with using smtp.gmail.com – Theodore Dunn Nov 03 '13 at 10:57

2 Answers2

6

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()
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • There was an error when the message is created using **msg=MIMEMultipart()**. Defining the message using **msg = MIMEText('body…', 'plain', 'utf-8')** as you suggested solved my problem and I am able to send body text in the message. Thanks. – Ahmed Gad Sep 25 '19 at 05:10
1

Add the following to your code before logging in and have a try again;

try:
        self.smtp.ehlo()
        self.smtp.starttls()
        self.smtp.ehlo
except:
        print "No TLS :("

#do login here
darxtrix
  • 2,032
  • 2
  • 23
  • 30