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

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:
bit.ly/python-emails
Google Account Settings:
myaccount.google.com/lesssecu...
myaccount.google.com/apppassw...
Environment Variables (Windows) - • Python Quick Tip: Hidi...
Environment Variables (Mac and Linux) - • Python Quick Tip: Hidi...
Lists Tutorial - • Python Tutorial for Be...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

Пікірлер: 730

  • @aqurds
    @aqurds5 жыл бұрын

    If you get hundreds of test email in your inbox, don't get disturbed! Be happy to know that hundreds of people implemented what they learned here... :) As always, Corey, you are one of the best in teaching python. Thanks for this contribution.

  • @cicerosilva-ye8mn

    @cicerosilva-ye8mn

    5 жыл бұрын

    hey man Where am I wrong ?? my code = = = import os import smtplib from email.message import EmailMessage EMAIL_ADDRESS = os.environ.get('EMAIL_USER') EMAIL_PASSWORD = os.environ.get('EMAIL_PASS') msg = EmailMessage() msg['subject'] = 'teste de email' msg['from'] = 'anymail@gmail.com' msg['to'] = 'anymail@gmail.com' msg.set_content('teste de emial enviado pelo script em python') with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(EMAIL_ADDRESS,EMAIL_PASSWORD) smtp.send_message(msg)

  • @motherfuqjohnson5460

    @motherfuqjohnson5460

    4 жыл бұрын

    @@cicerosilva-ye8mn what error? and what line is the error on?

  • @cicerosilva-ye8mn

    @cicerosilva-ye8mn

    4 жыл бұрын

    @@motherfuqjohnson5460 in the sccrip ModuleNotFoundError: No module named 'email.message'; 'email' is not a package

  • @cicerosilva-ye8mn

    @cicerosilva-ye8mn

    4 жыл бұрын

    at this from email.message import EmailMessage

  • @motherfuqjohnson5460

    @motherfuqjohnson5460

    4 жыл бұрын

    @@cicerosilva-ye8mn what IDE are you using? im using pycharm and i had to go into the interpreter settings to add the module. search up "how to add module to pycharm"

  • @carlwcampbell
    @carlwcampbell5 жыл бұрын

    Thank you, for putting out a video of something so useful that many Pythonistas ask.

  • @av4787

    @av4787

    4 жыл бұрын

    Pythonistas

  • @johnweller5491
    @johnweller54913 жыл бұрын

    Your videos are my lifeline!! I am a retired programmer trying to learn Python. I've not used any other language apart from FoxPro since using Pascal 30 years ago.

  • @officesuperhero9611
    @officesuperhero96115 жыл бұрын

    As always, another great video chuck full of practical tips. I have never seen a video that you made that I felt was useless or poorly made. Your videos are always top notch and the best.

  • @sageedwards7866
    @sageedwards78664 жыл бұрын

    For anyone that had trouble, I changed my port number from 587 to 25 and it fixed the 'gaierror' for me. Also huge thanks to you Corey!! Awesome, streamlined content.

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. check out here: al-taie.github.io/quick-mailer

  • @anglerfish61
    @anglerfish613 жыл бұрын

    What I like about this is, eventhough the code is solid it didnt work untill I understood what I was doing. Thanks for that. That is a valuable lesson

  • @mijimelon
    @mijimelon3 жыл бұрын

    thank you. you explained everything perfectly line by line and argument by argument

  • @rotrose7531
    @rotrose75313 жыл бұрын

    Your python tutorials are unfailingly amazing!

  • @marvinreyes7459
    @marvinreyes74593 жыл бұрын

    We need more people like you man!.

  • @martind4491
    @martind44913 жыл бұрын

    Corey, thank you very much for this video. It's extremely clear, fast pace, super useful. Thanks mate!

  • @lukerobertson1000
    @lukerobertson10003 жыл бұрын

    Thanks so much Corey! It's such a nice feeling to get all the code working and follow along. I really appreciate the list of contacts idea at the end, it seems so logical to do it that way and what can I say, I'm a big fan of logic! :) Thanks again! I'm loving your content and explanations.

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. al-taie.github.io/quick-mailer

  • @potschochterpotschochter2403
    @potschochterpotschochter24033 жыл бұрын

    I just bumped into your video. It is amazing. Thank you so much. I am so very new to Python and I think that this is one of the best tutorial I ahbe ever seen here on youtube. Thank you so much.

  • @prashanthenuganti736
    @prashanthenuganti7362 жыл бұрын

    Very clear and simple.

  • @rahesc
    @rahesc4 жыл бұрын

    this is really awesome and helpful. you simplify it so much. thank you!!

  • @palitfiq3742
    @palitfiq37425 жыл бұрын

    It works! Thank you.

  • @rewadia123
    @rewadia1233 жыл бұрын

    Thanks for explaining each type in detail

  • @JorgeEscobarMX
    @JorgeEscobarMX Жыл бұрын

    I loved this one, it seems so simple to send emails here.

  • @i5artube
    @i5artube5 жыл бұрын

    I can watch your videos on a tablet without getting blind... I'm going to thumbs up all your videos!

  • @sol5179
    @sol51794 жыл бұрын

    Super useful! Thank you.

  • @jodi8076
    @jodi8076 Жыл бұрын

    This tutorial was so simple and straightforward. Super helpful. AND it worked!! Thanks, will be sure to check out your other videos.

  • @nikolaosboutsioukis3945

    @nikolaosboutsioukis3945

    Жыл бұрын

    Bro this feature was turned off by google couple of months ago how you say that it worked?

  • @harsh3305
    @harsh33055 жыл бұрын

    Thanks a lot helped me generate automatic email notifications in my web scraping project !! PS- Puppy is so cute :D

  • @aakashjana6225
    @aakashjana62254 жыл бұрын

    I implemented it my flask back-end for sending automated mails to those who subscribe 😎. Thanks a lot Corey for this fantastic video.

  • @venkataramanansundaresan321

    @venkataramanansundaresan321

    3 жыл бұрын

    can you send the code for it i am also trying to do the same but dont know how to do

  • @aakashjana6225

    @aakashjana6225

    3 жыл бұрын

    @@venkataramanansundaresan321 yes but where? Where exactly are stuck? You gotta create a mailing function where you pass in the mail address and it sends a mail everytime someone hits subscribe button on the website with their mail ID filled in from the input box

  • @alexthewebdesigner1856
    @alexthewebdesigner1856 Жыл бұрын

    @Corey Schafer Thanx for posting such a comprehensive vid. None of the other videos on email via Python touched on Google settings regarding 2-step or TLS.

  • @rushibhagwat4108
    @rushibhagwat41083 жыл бұрын

    Thanks buddy you saved my project, I really appreciate your work.

  • @Xatex18
    @Xatex18 Жыл бұрын

    Thank you, this clarified some questions.

  • @ScottMorgan88
    @ScottMorgan883 жыл бұрын

    Thanks, Corey. That worked great. Note that now you need to set up a App Password if you're using a Google account. This is to get around the 2-step verification process.

  • @ashishpatil3416
    @ashishpatil34164 жыл бұрын

    Hey Corey, you are real tech nerd. Video is awesome.

  • @deliciafernandes9946
    @deliciafernandes99462 жыл бұрын

    Only youtuber I can trust with python content!

  • @johangodinho
    @johangodinho3 жыл бұрын

    Thanks a lot mate, I appreciate the effort you have put into making this video so easy to understand while also meeting the target of the intended title of the video. After looking online for a long time I finally came across your tutorial and I'm leaving with more information than I needed which is all so useful. So once again thanks a lot cus you made my day and all the best for the growth of your channel :))))

  • @alexvallex5487

    @alexvallex5487

    2 жыл бұрын

    do you know where in the code should i put by enviroment variable of the app password i

  • @danielrosas2240
    @danielrosas22403 жыл бұрын

    Thank u so much, Corey! Greetings from Mexico!

  • @shubhamhire9390
    @shubhamhire9390 Жыл бұрын

    This was such an amazing video, it took me a lot of time to pause and then understand and then apply it to the code, but it just made me learn so much , you are such an amazing teacher sir, thanks for sharing your knowledge with us.🙏🙏🙏

  • @traveldirt
    @traveldirt3 жыл бұрын

    Thank you man, worked first go!

  • @portfedh
    @portfedh3 жыл бұрын

    This was really helpful. Thank you very much!

  • @amirroohi4566
    @amirroohi45665 жыл бұрын

    Amazing channel, subscribing right away

  • @kamaldhungana6677
    @kamaldhungana6677 Жыл бұрын

    you explained things very nicely!

  • @claylanzino9749
    @claylanzino97493 жыл бұрын

    Very well. Thanks for Sharing.

  • @medizinmagdeburg544
    @medizinmagdeburg5444 жыл бұрын

    Thanks for your work!

  • @kayderl
    @kayderl4 жыл бұрын

    Thumbs up. Love all your vids, they are always so informative and useful.

  • @jitheshkuyyalil
    @jitheshkuyyalil2 жыл бұрын

    works like a charm, thank you

  • @darkbanditEX
    @darkbanditEX2 жыл бұрын

    Thank you Corey for another great video. Hope to see you back again in the future.

  • @Kiranhuded23
    @Kiranhuded233 жыл бұрын

    All your videos are "GOLD MINE" on the internet. I've learnt better with your videos than most others i've tried, I feel i owe you a lot , I'm making patreon contribution now!! Thanks a lott !!! god bless you Corey!!

  • @asega27

    @asega27

    3 жыл бұрын

    If this worked for you can you help me I hate to bother you

  • @victormaia4192
    @victormaia41923 жыл бұрын

    Thanks! This video made de subject more clear to me and I could follow the ressults

  • @mayoufmohamedanis5337
    @mayoufmohamedanis53377 ай бұрын

    Very intuitive !

  • @romankh5338
    @romankh53384 жыл бұрын

    Loving your work! Thank you!

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options check out here: al-taie.github.io/quick-mailer

  • @benko6171
    @benko61713 жыл бұрын

    This is so helpful you have noooooooooo idea. Thank you cory!

  • @destru1914
    @destru19142 жыл бұрын

    Thanks for this great tutorial!!

  • @igortechgameplays7114
    @igortechgameplays71143 жыл бұрын

    You're just amazing guy ! thank you so much!

  • @lydiaaidyl3328
    @lydiaaidyl33284 жыл бұрын

    Thanks a lot, especially for the HTML part, it was the only thing it worked for me after trying a lot of different options

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. check out here: al-taie.github.io/quick-mailer

  • @treaper
    @treaper5 жыл бұрын

    Oh man this video probably saved me hours of unnecessary struggle. If you could somehow get a link to this video as one of the top answers to google queries like "Python send email with attachment pdf" I bet you would get some extra traffic! Thank you so much for your work! I know that my learning process will be much smoother when I find one of your videos on the topic I want to learn. Youda best

  • @carltondaniel8966
    @carltondaniel89664 жыл бұрын

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

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. al-taie.github.io/quick-mailer

  • @VCodeIt
    @VCodeIt Жыл бұрын

    Thanks a lot, It is just splendid explanation.

  • @user-ml5pi1qt8s
    @user-ml5pi1qt8s3 ай бұрын

    Thanks for the video. Its was very helpful

  • @yashindane2844
    @yashindane28442 жыл бұрын

    Thanks for the amazing video ☺️

  • @martind4491
    @martind44913 жыл бұрын

    Got a new subscriber because of the content of this video!

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars3 жыл бұрын

    your channel is a gem mine :)

  • @rajdipshah532
    @rajdipshah5325 жыл бұрын

    thanks a lot man it really helped

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt5 жыл бұрын

    Thank u for this awesome video

  • @louiswallice3746
    @louiswallice37462 жыл бұрын

    like many of your videos, you got the best content and also every methods works :) thanks a lot mate!

  • @darklord7261
    @darklord72612 жыл бұрын

    This is sooo amazing and helpful thank you so much ❤️✨

  • @cuongtranmanh2287
    @cuongtranmanh22873 жыл бұрын

    great and easy to follow up

  • @aniketbhosale7418
    @aniketbhosale74183 жыл бұрын

    The Tutorial was very useful , it taught lot of stuff ,great work👍🏻 If anyone is getting Error for Environment Variable then just try by installing:- pip3 install python-dotenv then create '.env' file as it is mention your variable in that eg:- EMAIL_USER=abc@gmail.com EMAIL_PASS=abc@123 and python path lib USER=os.getenv('EMAIL_USER') PASS=os.getenv('EMAIL_PASS') just like that it's Done check by using print(USER,"",PASS).🤘🏻

  • @mrcrblr850
    @mrcrblr8502 жыл бұрын

    thank you for this amazing video

  • @tebbythomas1809
    @tebbythomas18094 жыл бұрын

    Excellent! Thanks

  • @jthomassaldias
    @jthomassaldias Жыл бұрын

    Corey, i have some problems running the code if I write it as shown, but when copied to my VS it does ran great. Thank you for this great tutorial.

  • @danielaramirez7923
    @danielaramirez79233 жыл бұрын

    Thank you so much Corey

  • @rakhshandamujib2793
    @rakhshandamujib27934 жыл бұрын

    Love it! Thank you for being so amazing!

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. check out here: al-taie.github.io/quick-mailer

  • @fundtrading

    @fundtrading

    Жыл бұрын

    Hello what are you blasting with 🤞

  • @ashu60071
    @ashu600714 жыл бұрын

    this was so amazing!!! loved all your videos. big fan

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options check out here: al-taie.github.io/quick-mailer

  • @serranomorante
    @serranomorante5 жыл бұрын

    Love you, man!

  • @shantanurauthan9318
    @shantanurauthan93182 жыл бұрын

    great tutorial thanks

  • @minathanh7072
    @minathanh70722 жыл бұрын

    Thank you for your video and pic sharing

  • @jayjain6204
    @jayjain62042 жыл бұрын

    Thanks a lot it works

  • @RaoAnilYadav
    @RaoAnilYadav Жыл бұрын

    Hello Corey, Thank you very much for this video. It's extremely clear, fast pace, super useful. BR, Anil

  • @chippandenga6722
    @chippandenga67223 жыл бұрын

    Thank you very much Corey. What could be the reason for someone to dislike such a valuable video?😂

  • @mseroglu47
    @mseroglu472 жыл бұрын

    thanks. the video is pretty extensive.

  • @younesboukroun3060
    @younesboukroun30605 жыл бұрын

    Thank You verry much body Respect for You

  • @anzu8698
    @anzu86984 жыл бұрын

    Thank you so much! 🙏

  • @VantedL
    @VantedL4 жыл бұрын

    Thank you so much this video is realy helpfull

  • @debjitchattopadhyay7627
    @debjitchattopadhyay76273 жыл бұрын

    Awesome tutorial!!! This video just gave a idea for a new project Building a Custom Mailing Enterprise Software now. Thanks a lot!!

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. al-taie.github.io/quick-mailer

  • @debjitchattopadhyay7627

    @debjitchattopadhyay7627

    3 жыл бұрын

    @@al-taie thanks 👍

  • @soumitralahiri100
    @soumitralahiri1002 жыл бұрын

    Excellent video Corey! The steps were very clearly explained and worked without any issues. One question - the example that you provide is for including multiple attachments to a single email; however, if I wish to include the list of attachments to individual emails (one attachment per email), what would be the best way to proceed? I have looked this up in details and not found a satisfactory solution. Will greatly appreciate your help with this.

  • @FarhanAhmed1360
    @FarhanAhmed13603 жыл бұрын

    Great tutorial

  • @a.wessel8865
    @a.wessel88652 жыл бұрын

    Great video, Corey! I am a total newbie to Python, and you made this so easy to follow. Could you possibly create a video or point me towards a resource where a Python script is used to send an HTML email with variable data from a SQL query? For example, if I wanted to email a simple summary report with the sum of deposit balances, deposit counts, loan balances, and loan counts with HTML formatting, could this be done with a Python script?

  • @Lexein
    @Lexein2 жыл бұрын

    this video is amazing

  • @Abdullahkhan-fz5ko
    @Abdullahkhan-fz5ko2 жыл бұрын

    Thanks so much

  • @Canda-fh4xc
    @Canda-fh4xc5 жыл бұрын

    Thank you Corey for the video. It will be really nice if you can make a video about how to read emails from Gmail

  • @yltfy
    @yltfy4 жыл бұрын

    Another wonderful video!

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. check out here: al-taie.github.io/quick-mailer

  • @user-ns8ur8yh8r
    @user-ns8ur8yh8r4 жыл бұрын

    you are genius..........thank a lot of about what you doing with your lightfully teaching..... you are one of the best teacher in this world...god bless you and your family too....................you are a light for our brains

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options. check out here: al-taie.github.io/quick-mailer

  • @sssrigoku
    @sssrigoku5 жыл бұрын

    Thanks for the video

  • @arashabolhasani7596
    @arashabolhasani75963 жыл бұрын

    Great, Thanks

  • @user-pp6tv9jh6g
    @user-pp6tv9jh6g3 жыл бұрын

    thank you!

  • @tamannagoyal8953
    @tamannagoyal89535 жыл бұрын

    woow great god bless ya sir

  • @ilkinsan
    @ilkinsan3 жыл бұрын

    thank you so much

  • @wilberrosales6345
    @wilberrosales63452 жыл бұрын

    YOU ARE THE GOAT COREY

  • @buschleuchte9905
    @buschleuchte99053 жыл бұрын

    you saved alot of nerves

  • @jesusloaiza3032
    @jesusloaiza30323 жыл бұрын

    Thanks Corey!!

  • @scifregizmoguy
    @scifregizmoguy5 жыл бұрын

    *Question on HTML email:* *[SOVED]* Is there a way to send a HTML email without having to write it manually in triple quote, but more automated? Like if I had a file called "email.html", and I didn't want to send it as an attachment, but the email itself. Thanks, anyone. EDIT: I found a solution! If anyone is interested here it is; ### with open('my_HTML_file.html', 'rb') as f: file_data = f.read() file_type = imghdr.what(f.name) file_name = f.name #print(file_data) file_string = file_data.decode(encoding='UTF-8') #print(" ::: The file decoded into a string ::: ", file_string) msg.add_alternative(file_string, subtype='html') ###

  • @diplomat1837

    @diplomat1837

    4 жыл бұрын

    Where does this code comes in the code. I’ve been having alternative error when I tried sending the HTML link on the code. Please some help to get me through

  • @ajith_e

    @ajith_e

    3 жыл бұрын

    Hey, Thanks man !! I was just searching for the same. I have an external html file read into the script. That was semantic

  • @dnetne5508
    @dnetne55084 жыл бұрын

    Thanks

  • @coreycarter5668
    @coreycarter56685 жыл бұрын

    You make me so happy! Can’t wait to watch this video lol

  • @arklan7
    @arklan73 жыл бұрын

    Excellent!

  • @josepcirerario3172
    @josepcirerario31724 жыл бұрын

    thanks for this, very helpful.

  • @al-taie

    @al-taie

    3 жыл бұрын

    You can use (quick-mailer) module, to send email in two lines of code, with a lot of options check out here: al-taie.github.io/quick-mailer

  • @sandeepvk
    @sandeepvk5 жыл бұрын

    keep doing good stuff !