Unleash the Power of Python for Effortless Email Sending!

Handling your emails.


Sending emails in everyday life is quite an easy task using email applications or web interface. But sometimes you need to automate your routine tasks and distribute information right from the place where it was generated. Thats where Python should be used !!!

Sending emails with Python.
Sending emails with Python meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-11-20 by Andrey BRATUS, Senior Data Analyst.




    Simple but efficient scripts below are ready to help, just substitute example data with your own.

    IMPORTANT!!!: you can specify password information in explicit way, but it's not recommended for security reasons. You can also store password in external file, tnen import it in a script and use or store it in environment as shown below.


  1. Sending single e-mail with Python:


  2. 
    import yagmail
    import os
    
    sender = 'my_example@gmail.com'
    receiver = 'my_example@python-code.pro'
    
    subject = "Crazy example subject!"
    
    
    contents = """
    For best jokes on the web visit https://python-code.pro !!!
    """
    
    yag = yagmail.SMTP(user=sender, password=os.getenv('PASSWORD'))
    yag.send(to=receiver, subject=subject, contents=contents)
    print("Crazy Email Sent!")
    

    OUT: Crazy Email Sent!



  3. Sending an e-mail periodically with Python:


  4. 
    import yagmail
    import os
    import time
    
    sender = 'my_example@gmail.com'
    receiver = 'my_example@python-code.pro'
    
    subject = "Crazy example subject!"
    
    
    contents = """
    For best jokes on the web visit https://python-code.pro !!!
    """
    while True:
      yag = yagmail.SMTP(user=sender, password=os.getenv('PASSWORD'))
      yag.send(to=receiver, subject=subject, contents=contents)
      print("Crazy Email Sent!")
      time.sleep(60)
    

    OUT: Crazy Email Sent!



  5. Sending an e-mail at a certain time with Python:


  6. 
    import yagmail
    import os
    import time
    from datetime import datetime as dt
    
    sender = 'my_example@gmail.com'
    receiver = 'my_example@python-code.pro'
    
    subject = "Crazy example subject!"
    
    
    contents = """
    For best jokes on the web visit https://python-code.pro !!!
    """
    while True:
      now = dt.now()
      if now.hour == 23 and now.minute == 59:
        yag = yagmail.SMTP(user=sender, password=os.getenv('PASSWORD'))
        yag.send(to=receiver, subject=subject, contents=contents)
        print("Crazy Email Sent!")
        time.sleep(60)
    

    OUT: Crazy Email Sent!


  7. Sending an e-mail to contact list (in CSV):


  8. 
    import yagmail
    import os
    import pandas
    
    sender = 'my_example@gmail.com'
    
    subject = "Crazy example subject!"
    
    
    yag = yagmail.SMTP(user=sender, password=os.getenv('PASSWORD')) 
    
    df = pandas.read_csv('my_contacts.csv')
    
    for index, row in df.iterrows():
      contents = f"""
    Hi {row['name']} .For best jokes on the web visit https://python-code.pro !!!
    """
      yag.send(to=row['email'], subject=subject, contents=contents)
      print("Crazy Email Sent!")
    

    OUT: Crazy Email Sent!
    Crazy Email Sent!
    Crazy Email Sent!


  9. Sending an e-mail with attachment:


  10. 
    import yagmail
    import os
    
    sender = 'my_example@gmail.com'
    receiver = 'my_example@python-code.pro'
    
    subject = "Crazy example subject!"
    
    contents = ["""
    For best jokes on the web visit https://python-code.pro !!!
    """, 'attachment.txt']
    
    yag = yagmail.SMTP(user=sender, password=os.getenv('PASSWORD'))
    yag.send(to=receiver, subject=subject, contents=contents)
    print("Crazy Email Sent!")
    

    OUT: Crazy Email Sent!





See also related topics: