Python – Send e-mails with Python

Sending e-mail through the internet is quite a trivial trick in many programming languages. Some time ago, I have shown how to send MS Excel range as an e-mail and how to send a mail through VBA, with HTML.python-logo-master-v3-TM

As far as now I am interested in Python, a few days ago I have developed a simple banking software system, as a respond of this task. One of the features of the bank system, was to send a password with a hashed code, if the original password was forgotten. Thus, I have registered a new account in Yahoo.Mail and I have started to spam myself in a way to learn how to do it. 🙂

After some research I found three things:

  1. There is a library called smtplib, which helps you a lot in the idea!
  2. If I am going to publish this in the internet, it is a great idea to import the username and the password from another folder. Thus, in my case, these are imported from settings.
  3. The mails are received about 2 to 3 minutes after they are sent, but this was time I was willing to wait.

At the end, I have received managed to receive something like this:

export

Well, that was my target, so I managed it! 🙂

At the end, as usual, here comes the self-explanatory code:

import smtplib
from email.mime.text import MIMEText
from settings import SMTP_USERNAME, SMTP_PASSWORD


def send_mail(username, password, mail=SMTP_USERNAME):

    SMTP_SERVER = "smtp.mail.yahoo.com"
    SMTP_PORT = 587
    EMAIL_FROM = SMTP_USERNAME
    EMAIL_TO = mail
    EMAIL_SUBJECT = "Test Mail - Password reset Money-In-The-Bank Project"
    co_msg = """
    Disclaimer:
    Please, consider this mail just a test mail with educational purposes, if by any mistake you receive it. Simply delete it.
    I am sorry! :)

    Hello, {}!
    You have requested a password reset.
    Please, enter the following password for password:
    (between the ">>>" and the "<<<" signs below):


    >>>{}<<<.


    Have a great day!

    Best Regards,

    Money-In-The-Bank Training Team
    """.format(username, pas
 

sword)

    msg = MIMEText(co_msg)
    msg['Subject'] = EMAIL_SUBJECT
    msg['From'] = EMAIL_FROM
    msg['To'] = EMAIL_TO
    debuglevel = True
    mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mail.set_debuglevel(debuglevel)
    mail.starttls()
    mail.login(SMTP_USERNAME, SMTP_PASSWORD)
    mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
    print("A new mail has been generated and sent to {}.".format(EMAIL_TO))
    mail.quit()

send_mail(SMTP_USERNAME, SMTP_PASSWORD)

A few notes on the code:

  1. You may decide to set “debuglevel” to “False”, in order to miss the 20 lines of interaction with the yahoo servers.
  2. In the current code, you are sending an e-mail to yourself. In my case jobanana500 is sending to jobanana500. I did this, with the idea not to spam someone by mistake. It is rather easy to find a way how to send a mail to someone else.
  3. Last but not least, do not forget that spamming is a crime!

That’s all folks! 🙂