How to send an Email and attachments with Python
In the past I’ve noticed several blogs offering articles on how to send emails with Python. Until now had I passed over them, as I was only vaguely interested (and without even looking), decided it would probably be too complicated for me.
But, in my quest to find some new code for my next Newb Code Snippets post, I thought I had better take a look.
Okay, some blog posts made it sound complicated, but I got a great pointer from one post, that recommended the YAGMail module, so I took a butchers at it.
So, brace yourselves, I am going to guide you through the fairly simple process of sending emails to anyone, with attachments, if required, using your GMail account. If you do not have a GMail account, it takes one minute to get one.
As is usual with me though, I managed to produce every error you could possibly make before I actually got it working properly. Hopefully I will save you some head scratching time here. Hey, maybe I should be a software tester or something, I can break any code.
First off, you will need to do a “pip install yagmail“.
I initially did as the YAG docs outlined on the yag github page and did “pip install yag [all]“, which also installs “keyring“, which allows you to keep your Email password encrypted on your drive. Sounds good, and it works, but the drawback is you have to type your password in every time you run your code, I think!
Also, as soon as I had problems, which was straight away, I didn’t know if the problem was with keyring, YAGmail, my code, or GMail, so I uninstalled keyring and went for the route one simple approach, (KISS, Keep It Simple, Stupid).
Once I had a simple script written, garnered and butchered from several sources, I tried to send a mail to my one of my other Email addresses and I got a
smtplib.SMTPAuthenticationError: (535, b’5.7.8 Username and Password not accepted.” error.
This was weird, as I was certain my password\username was correct.
Note: You get this same error if you do a stupid typo, like leaving out the @ in your gmail address for example, just saying.
Aha, I then realised I had my VPN running, sometimes Google throws a fit with my VPN, sometimes not, so I disconnected and used my real IP. But this still didn’t resolve the error.
After a few more failed attempts, a mail arrived from our friend Mr Google, explaining that my GMail account had been blocked because of suspicious activity. I was allowed to unblock it thankfully. Why do security tools seem to hate Python so badly?
Google also advised that if I wanted “Access for less secure apps“, then I had to go to my GMail settings and activate it. Which I did, the script then worked exactly how it should. Phew.
Double-click inside code box to select and copy
''' Send email with image attachment using gmail smtp You may need to pip install yagmail ''' import yagmail # set up your gmail credentials. # any problems could be gmail blocking you or wrong pw etc. # I had to allow unsafe apps in my gmail security # settings to get this to work. YAG_SMTP = yagmail.SMTP(user="your@gmail.com", \ password="your-password", host='smtp.gmail.com') # email subject SUBJECT = 'Yagmail Test' # email content with attached file from current dir. CONTENTS = ['Hi Dude', 'image attached innit.', 'some-image.jpg'] # send mail YAG_SMTP.send('person@anymail.com', SUBJECT, CONTENTS)
Okay, it’s pretty simple to use this script, but you need to fill in your details.
- Edit “user=”your@gmail.com” to be your Gmail address.
- Edit “password=”your-password”
- Edit SUBJECT = ‘Subject line of mail’
- Edit ‘Hi Dude’, ‘image attached innit.’ to your message.
- Edit ‘some-image.jpg’, if you want to attach an image.
- Edit ‘person@anymail.com’, to the Email address you want to send to.
- run the script, check the mail was sent, if so, clap hands, if not, kick cat.
I ran five successful tests with this code just before I started writing this article.
But I forgot to investigate two things that I wanted to look in to. How to make a longer text body, and I wanted to test other attachments like .pdf and ,mp3 files.
But when I went back to the code, Google had blocked me again, and this time there is no Email from them to unblock me, dear oh dear, some mothers do ‘ave ’em.
So I am going to leave this post here as I don’t want my real IP flagged as a spammer or something.
I’m pretty certain that you can have long body texts and send most common types of attachment, so you should be okay there, if that’s what you want to do.
The code above will be in my next code snippets post, so I will try testing it again, and improving the code, when I write that post, but I shall sign off for now.
Edit: All Working again, this is is weird, it’s probably all my fault somehow. I successfully sent ,pdf and .mp3 attachments, no problemo. As for the body text, each line of text should be enclosed in quotes and comma separated. We are all done here.
Note: Regarding the first image. This is called the Pinna-Brelstaff illusion, use the bigger picture that is used in this Reddit post link. Stare at the picture for a few seconds, then move your head slowly towards the image and the circles will appear to turn clockwise. Pull your head away slowly and the motion reverses. I got a right old migraine editing that image!
I’m using Python V3.6.5 32bit, on Windows 7 64bit
Previous Post: PyTips – Python For Windows
I have also started another free blog of my personal Memoirs, that nobody will be interested in, except maybe my son and my mum
“If one does not leave a record of one’s doings on Earth, then what is the point of it all?” Steve Shambles. 2019
from Python Coder
via read more
No comments:
Post a Comment