Sending email with python smtplib not working, confused about the "from" field

19,881

Solution 1

This runs without error, but no email is sent to [email protected].

This usually means, the message was transferred to your MTA (mailserver) on 'localhost', but this server could not relay it to gmail. it probably tried to send a bounce message to "[email protected]" and that failed as well. or it sent the message successfully but it landed in gmails spam folder (the message could trigger spam rules since it is missing a date header)

One thing I don't understand about this code --- what restrictions do I have when setting the FROM field?

it must be a syntactically valid email address

Do I somehow have to say that it was from my computer?

no. but that could be the problem why it was not delivered. is your computer on a home/dynamic/dial-up IP? gmail (and many many many other providers) don't accept mail from such IPs. the HELO of your mailserver might be wrong, DNS settings might be incorrect etc. you need to check the server logs. you probably have to configure your local mailserver to relay the message via a smarthost instead of trying to contact the target server directly.

What is in place to prevent me from spoofing someone else's email?

not much, that's why we have so much spam from forged adresses. things like SPF/DKIM can help a bit, but the SMTP protocol itself doesn't offer protection against spoofing.

Or am I at liberty to do that?

technically yes.

Solution 2

Well, since you don't specify exactly what kind of email server you are using and its settings, there are several things that might be wrong here. First of all, you need to specify the HOST and the PORT of your server and connect to it. Example:

HOST = "smtp.gmail.com"
PORT = "587"
SERVER = smtplib.SMTP()
SERVER.connect(HOST, PORT)

Then you need to specify an user and his password to this host. Example:

USER = "[email protected]"
PASSWD = "123456"

Some servers require the TLS protocol. Example:

SERVER.starttls()

Then you need to login. Example:

SERVER.login(USER,PASSWD)

Only then you are able to send the email with your sendmail. This example works pretty well in most common servers. If you are using, as it seems, your own server, there aren't much changes you need to apply. But you need to know what kind of requirements this server has.

Share:
19,881
WillHaack
Author by

WillHaack

Updated on July 05, 2022

Comments

  • WillHaack
    WillHaack almost 2 years

    I'm trying to send an email in python. Here is my code.

    import smtplib
    
    if __name__ == '__main__':
    SERVER = "localhost"
    
    FROM = "[email protected]"
    TO = ["[email protected]"] # must be a list
    
    SUBJECT = "Hello!"
    
    TEXT = "This message was sent with Python's smtplib."
    
    # Prepare actual message
    
    message = """\
    From: %s
    To: %s
    Subject: %s
    
    %s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    
    # Send the mail
    
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO[0], message)
    server.quit()
    print "Message sent!"
    

    This runs without error, but no email is sent to [email protected].

    Questions

    One thing I don't understand about this code --- what restrictions do I have when setting the FROM field?

    Do I somehow have to say that it was from my computer?

    What is in place to prevent me from spoofing someone else's email?

    Or am I at liberty to do that?