To code a script to send emails could be very useful for reporters. We could use it to create personalized alerts about any topic we want or to monitor our running scripts at a distance. Note: To send yourself an email with a Python script, you need minimal knowledge of programming. I made for people willing to learn. Once done with the tutorial, you should be able to use the code I wrote below! I Personalized alerts By sending an email to yourself with the conditions of your choice, as coded in your script, you can create completely customized alerts. You want to closely monitor an elected representative? Code a script that will check his Twitter and Facebook accounts every 10 minutes. If something new has been posted, an email will be automatically sent to you! Your program could check three times a day all the transcript files of all the meetings he is supposed to attend. Google Alerts are a joke compared to this! II Monitor you scripts at a distance Another interesting use: to indicate when your scripts are done or when they stopped for an unexpected reason. Some scripts can run for a very long time… If you are a web scraping addict, you know what I am talking about. If there is a lot of data to extract, your scripts will give your grey hair! The goal was to extract all the data from the pro-active disclosure section of. Thousands of contracts, all on different webpages…! On top of that, I was regularly disconnected from their server. It took me days! Hopefully, email updates could avoid you a lot of stress. No need to install external librairies! To send a basic email without subject line , with a Gmail address, the code is fairly simple: server. First, the server location or its ip address , then the port to use. If you have an email address from another service, like Yahoo for example, you have to find the corresponding information. It will protect your password. Donc forget to indicate your email address and your password on line 5. The msg variable will contain your message and the last ligne will send it! With the code below, you will send a clean email, with a sender, a receiver and a subject line. To do this, you need two more modules:. They are part of the basic Python librairies. No need to install them. MIMEMultipart import MIMEMultipart from email. While doing some research, I noticed almost everybody has a slightly different method to do this my sources are all at the end of this article. I tried to keep the code as simple as possible. However, many modules are still needed, as you can see below. In summary, the essential step is to convert the file into a before sending it. My code works for text files, pdf files, images, audio files and video files! MIMEMultipart import MIMEMultipart from email. MIMEText import MIMEText from email. You have everything you need to add email sending to your scripts! Follow me on , and to know when I will publish new articles! How did I learn to send emails with Python? Geez louise, with reporting applications too. No need to install external librairies! Well, now you know! Thank you very much Nael. But i faced a problem. What happened to me? How can I fix this? Google makes you change your settings, even with server. I know even if you have a 2 step authentication on your google account, you can set up a password for an app. It only supports Oauth 2. Through this authentication method, username and password are not stored , instead used once for authentication and further authorization take place through tokens issued by google to the third party app. This reduces the risk of compromise of passwords, when the third party apps are compromised for some reason. No need to use passwords. I tried it and it works. The attachment is in the root directory. You have to give the path to a file, with an extension. Let me know if it works! Can you please tell me the IDE you use to run this basic email. I use Python 3 IDLE and plug in all the relevant info into your script and it does not work. I get error smtplib. It looks like in Python 3, the modules are called email. But still, I get the same error. MIMEMultipart import MIMEMultipart from email. I was slow to answer. Happy you found the solution by yourself! Could you fix this error on your post? That would be awesome! I also think you should change it to what k said as i had the same problem and solution as him. Maybe it might be worth noting in the article so that others dont have a hard time the way I did figuring out why I was getting the error. Very concise and broken down into parts that are easy to understand and progress through the different concepts. I know how hard it can be to start learning all this stuff. I am glad it can help! The file is being created fine, and the copy that I create has everything I need it to have. However the text file that is attached to the email is blank when I receive it. Any idea why this would be happening? The attachment is no longer blank. Any idea why this would happen? If not Gmail will reject the mail. Could you please help? It looks like that Gmail made some change recently. You now have to allow you account to be accessed by less secure apps. I tried to set msg. Is it even possible from python script? It was very useful :D! But a good thing to do is to update the post to warn about permissions that must be changed, the gmail account of the sender must change e-mail configurations. Was wondering what tools you used to build your personal site. I really like the simplicity of this site. Do you use a static site generator like Jekyll or Pelican? Do I need to install something? I am on windows 8, and have Python 2. Any help would be greatly appreciated! Your work has helped me to quickly develop a system send daily results on this data to my portable devices via email. I am glad it helped! I just bought a Raspberry Pi. I am using proxy settings of my college. Kindly help me in this regards. How about how to send an email with an Excel file as an attachment? Well written, logically constructed. Multiple receivers, attachments, html emails, images etc. I made some mods to your basic script here: They are based on the basic script from Web Scraping with Python, by Mitchell. Could you explain, how to send email with python smtplib emails not only from Google but from other services too? But i would have enjoyed it more if in the end you had added one more section where you added an implementation with a class. But got an error. Errno 101 network unreachable. Could you please help? I really appreciate it!!!. The script for sending email has worked fine. Unfortunately, their scripts were helpless due to poor explanation. Please, how do I send sms using python script? I am new to Python. Tried sending an email with an attachment but got stuck. Any help would be appreciated. After reading the code, I write my own program and it works well. What could be the problem and a possible solution? Worked for me Great Job.. However, I did have to add two lines of code to make the authentication work properly with hotmail smtp. I am now able to send images to my email but I cannot view them once i download them from email. How do i view them or how do i send images that would be viewable? Did you solve it? When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, awesome blog! I like and love Python programming. This is a part of my life. This tutorial is very-very useful for programmers. Just a reminder: as I use Python 3. Thank you very much. You made my day! As the approach mentioned in your tutorial but i am getting server login error Is it because of smtp Do i need to setup different protocol and port. I work for a telecom company. Any ideas how to add multiple attachments to the same email? MIMEMultipart import MIMEMultipart ImportError: No module named MIMEMultipart I am usin python 2. Total python newbie, although not a newbie programmer. The examples go right to the point the explanations clear. Thanks for taking the time and effort to get this out there. How do i save the sent email to the sent items folder. Just one comment, in section V To send an email with attachment line 5 from email import encoders and line 25 encoders. Keep the good work. Worked right out of the gate. Thank you so much. I tried multiple websites and tutorials trying to send an email with python and yours is the only one that worked. Your article helped me very much. Thenk you for this. It worked for me. I just wonder how is this process faster than sending e -mail the normal way. I am new to this so I would like to know the merits of this process. I would also love to know how to sort mails and organizing my mail box using python. If you have any tutorial for such, please point me to the right direction. The only problem in my case that it only works with gmail server. I have a virtual private server wpxxxxxxxx. I thought that because of SSL, but I used smtplib. So I really would like to know the reason why. It really helped in understanding the modules. It is really easy to add the additional security though. Instead of using smtplib. SMTP simply use smtplib. I tried it with my Google account and it works. I did have to get an authentication code from the Google Dashboard though since I have two step verification. This post was extremely interesting, especially since I was looking for thoughts on this topic last Tuesday. By the way awesome piece of code! It was a great tutorial for a beginner. I want to attach multiple files which are saver at different locations. How do I do that? Its really remarkable paragraph, I have got much clear idea on the topic of from this paragraph. Nael Shiab for this contribution! I appreciated it a lot, I found it iteresting, usefoul and very well explained!! Excellent and simple post for sending email. But attachment part is not working. In fact, I m not getting any error also but the attachment does not occur. On ne garde aucune info.