Hi,
I am working out a program which involves emailing through a SMTP host I dont have control over... I can mail through PHP code but cant do so through python, because I get the following error

Traceback (most recent call last):
File "Teshting.py", line 7, in <module>
smtp.sendmail('Sender','thenameizprayag@gmail.com','Test')
File "/usr/lib/python2.6/smtplib.py", line 709, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'thenameizprayag@gmail.com': (554, '<thenameizprayag@gmail.com>: Relay access denied')}

My code is simple for now...

import smtplib

smtp = smtplib.SMTP()

smtp.connect(' MY SMTP HOST ')
smtp.sendmail('Sender','thenameizprayag@gmail.com','Test')

print "Mail sent successfully"

I would be grateful for a quick solution... I have tried a lot on the internet to no avail...

I haven't found the smtplib module to be super reliable, but here's what I use to send email:

def email(usertuple,sender,to,subject,msg):
    """Sends an email from a gmail account"""
    (login,pswd) = usertuple
    print 'Connecting...'
    mailServer = smtplib.SMTP("smtp.gmail.com", 0)
    print 'Initiating relationship...'
    mailServer.ehlo()
    print 'Encrypting...'
    mailServer.starttls()
    print 'Initiating encrypted relationship...'
    mailServer.ehlo()
    print 'Logging in...'
    mailServer.login(login,pswd)
    print 'Mailing stuff...'
    message = """\
From: %s
To: %s
Subject: %s

%s
""" % (sender, to, subject, msg)
    mailServer.sendmail(login,to,message)
    print 'Quitting...'
    mailServer.close()

You have to login to your gmail account, and it has to have smtp enabled. usertuple should be equal to: usertuple = ([i]your email[/i],[i]your password[/i]) Additionally, don't try to send mass amounts of emails, or emails right after each other, gmail will automatically ban you. :) We thought it would be funny if we filled up a friends email account with random emails about football, but gmail didn't. :)

The important note about the above solution is that when calling SMTP() zachabesh initializes it to connect to gmail's smtp server ( smtplib.SMTP("smtp.gmail.com", 0) ).

Python's smtplib relies on you having access to an existing smtp server or else it cannot work. Alternately, you can initialize SMTP in the way that you did previously and then use the connect method to connect to the server. Refer here for more info on this library (particularly the SMTP objects section)

This article has been dead for over six months. Start a new discussion instead.