How to Send Emails Using Python – Plain Text, Adding Attachments, HTML Emails, and More

///How to Send Emails Using Python – Plain Text, Adding Attachments, HTML Emails, and More

How to Send Emails Using Python – Plain Text, Adding Attachments, HTML Emails, and More

FavoriteLoadingAdd to favorites

In this Python Programming Tutorial, we will be learning how to send emails. We’ll start with simple plain text emails and then learn how to construct more advanced messages. We will learn how to attach images, PDFs, create HTML messages, and more. Let’s get started…

The code from this video can be found at:

Google Account Settings:

Environment Variables (Windows) –
Environment Variables (Mac and Linux) –

Lists Tutorial –

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2020-01-28T01:28:12+00:00January 28th, 2020|Python Video Tutorials|43 Comments

43 Comments

  1. Pedro N. January 28, 2020 at 1:28 am - Reply

    I am getting: 'SMTPServerDisconnected: please run connect() first'. How can i fix that ?

  2. Akash Dutt January 28, 2020 at 1:28 am - Reply

    Easy to understand tutorial. Very well explained. Thank you for the video.

    If someone still has issues after watching the video visit [here](https://cppsecrets.com/users/158011010511510499104105110116103484964103109971051084699111109/Python-SMTP-Module.php). That's a very basic article and author explains the basics very clearly.

  3. ghanshyam zambare January 28, 2020 at 1:28 am - Reply

    what if I have to call this as a function only when a certain condition is true..?

  4. Ashutosh Awasthi January 28, 2020 at 1:28 am - Reply

    hi can you plz let me how to send excel data with format in mail body by python

  5. Nearco1000 January 28, 2020 at 1:28 am - Reply

    @CoreySchafer, how can I capture sql output and embed it in a html in my email?

  6. kedarisetti siva January 28, 2020 at 1:28 am - Reply

    Send an HTML email with embedded video in python, can you please hel me this

  7. Vihari Peeta January 28, 2020 at 1:28 am - Reply

    Hi, I am unable to send mail to to multiple recipients, which I wanted to keep two or more recipients in TO, CC and BCC. Can you please share how this works and what need to done?

  8. afolabi sunday January 28, 2020 at 1:28 am - Reply

    Thanks Corey, your videos has helped Me alot as a self taught programmer, will be exciting to see you do videos on Blockchain, Maybe Stellar Blockchain.

    Thanks Soooooo much

  9. Misanthrop xX January 28, 2020 at 1:28 am - Reply

    Is it possible to use a database for the recipient email?
    So that i can send the same email to all my customers?

  10. scandalos22 January 28, 2020 at 1:28 am - Reply

    Amazing and explanotory video, man! Could you recommend a way to do the same, but for a hotmail account?

  11. rundown thomas January 28, 2020 at 1:28 am - Reply

    what happens when you want to send multiple lines of text as plain text ? also how to send a plain text email with a html logo at the bottom (ie ..company logo and details)

  12. CodingUniversity January 28, 2020 at 1:28 am - Reply

    Instead of .starttls() and .ehlo, you can use smptlib.SMTP_SSL. Right ? and the port number need to be 465

  13. innovia work January 28, 2020 at 1:28 am - Reply

    please Data structures and algorithms

  14. Anthony O'Brien January 28, 2020 at 1:28 am - Reply

    Great video – one question – if you are adding a .txt file or a .doc file what do you change maintype and subtype to. Thanks

  15. Anita Ma January 28, 2020 at 1:28 am - Reply

    Hey! For a project I need people to send me pictures. I would like to autoreply to emails with a specific subject a 'Thank You!' do you have any suggestions on what to use for this? All the best!

  16. Aara Dhanah January 28, 2020 at 1:28 am - Reply

    Thanks man!!!

  17. Mazeen Muhammed January 28, 2020 at 1:28 am - Reply

    Hey it would be nice if you could also tell us how to send mail within a proxy settings. Currently I work for a company and i am unable to send emails cause of proxy issues and i get a connection attempt failed because connected host failed to respond error.
    Thanks in advance .. 😉

  18. Muhammad Saepudin January 28, 2020 at 1:28 am - Reply

    Helpfull👏 thanks sir!

  19. inarisound - Anime Music & Memes January 28, 2020 at 1:28 am - Reply

    Hi Corey, do you know, is it possible to send scheduled messages?
    So let's say you run script today… but message will arrive tomorrow?
    I can't seem to find any examples on the surface… is it gmail functionality only… and we would have to use API to do that?

    thank you for the video <3

  20. Arne Gewert January 28, 2020 at 1:28 am - Reply

    thank you so much!

  21. inarisound - Anime Music & Memes January 28, 2020 at 1:28 am - Reply

    instructions unclear, EMAIL_RECEIVER set to CoreyMSchafer xD

  22. Makha Sy January 28, 2020 at 1:28 am - Reply

    This is great !!!

  23. edward oladigbolu January 28, 2020 at 1:28 am - Reply

    thank you corey schafer, been enjoying your youtube videos soo much. However, i would like any training of yours on building a payment gateway on a website using flask..or would appreciate any recommendation. thank you

  24. obed sayyad January 28, 2020 at 1:28 am - Reply

    Hey Corey would make a video on how to send html emails via sendGrid?

  25. Sanjay Hegde January 28, 2020 at 1:28 am - Reply

    hey, the email sent will be in the spam folder of the recipient. Why?

  26. hasaji rohit January 28, 2020 at 1:28 am - Reply

    socket.gaierror: [Errno 11001] getaddrinfo failed
    im getting this error plz help me out

  27. SIDDARTH SHESHAGIRI January 28, 2020 at 1:28 am - Reply

    How to insert image in the body of the email in outllook?

  28. The Viper January 28, 2020 at 1:28 am - Reply

    damn python you are sexy

  29. Guilherme Figueira January 28, 2020 at 1:28 am - Reply

    Thank you so much! 🙏

  30. Rick Klug January 28, 2020 at 1:28 am - Reply

    does this still work? I have 2 step authentication off and have Less secure app access on but still cannot get past "smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted."

  31. Rohit Venkat Gandhi Mendadhala January 28, 2020 at 1:28 am - Reply

    I always get this error when I use your code with Outlook SMTP hostname and Port
    raise SMTPAuthenticationError(code, resp)

    smtplib.SMTPAuthenticationError: (535, b'5.7.8 Error: authentication failed: authentication failure')

  32. Geeky Gamer January 28, 2020 at 1:28 am - Reply

    When I try to send HTML emails I get the HTML as text in the emails!

  33. jameel saeb January 28, 2020 at 1:28 am - Reply

    import smtplib

    with smtplib.SMTP('smtp.gmail.com',587) as smtp:

    smtp.ehlo()

    smtp.starttls()

    smtp.ehlo()

    smtp.login('my email','my passwd')

    subject = 'heelloo'

    body = 'vak'

    msg = f'subject: {subject}nn{body}'

    smtp.sendmail('my email,the recever email', msg)

    what is wrong with it, it shows me (sendmail() missing 1 required positional argument: 'msg') when I function it

  34. Dennis Asamoah January 28, 2020 at 1:28 am - Reply

    Wonderful

  35. carlton daniel January 28, 2020 at 1:28 am - Reply

    Thank you sir, it helped me understand sending email in a simple way

  36. Raghul Palani January 28, 2020 at 1:28 am - Reply

    Hi Corey,

    How to copy a Excel(.xlsx, .xls) data to the body of the email?

  37. sqeedward January 28, 2020 at 1:28 am - Reply

    Nice video m8 🙂

  38. Jis Mathew January 28, 2020 at 1:28 am - Reply

    Corey can you include videos on checking out your mail inbox through python ?

  39. Shelly Gong January 28, 2020 at 1:28 am - Reply

    Can anybody solve this problem?
    [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

  40. Medizin Magdeburg January 28, 2020 at 1:28 am - Reply

    Thanks for your work!

  41. ricardo sagrero January 28, 2020 at 1:28 am - Reply

    25:40 for PDF

  42. sol :) January 28, 2020 at 1:28 am - Reply

    Hey @Corey Schafer, just curious. Is it possible to include python variables inside HTML email? I wanted to make email template for different situations. For example, if my script succeeds, it would send like: "Hey script run successfully". If not "Hey there was a problem running the script" and email body would include that error message.

  43. GAMESHAKER January 28, 2020 at 1:28 am - Reply

    imghdr.what returns none. Please can anyone help me here?

Leave A Comment

*