In this video, you will learn how to send out an email that has an attachment with Python.
Let's see how we can do that.
For that, I'm going to fork
perhaps the Send Single Email report.
Since Email with Attachment.
So let's do some things first.
Set a password, a key for the Gmail account, a value.
That's my app passwords.
And press on add new secrets, then the next thing we want to do is let's create a sample file to attach
to our email, text.txt.
Just some content.
That's it.
Text.txt is the file we want to attach.
And then the receiver.
So we're going to send a single email at this time.
The receiver is going to be this one in here.
So on dropmail.me.
You can have disposable email accounts, paste it there.
Sending an email with an attachment is easier than you think.
All you have to do is modify the contents variable.
So, what we have to do is we need to convert this string into a list or to see it more
accurately,
you need to put to this string inside a list like that.
Now we have a list containing one string, which is this one.
But if we add one more item to this list, which could be like text.txt, so the path to this
file and then we send out the email.
Then Python or more specifically, yagmail, this library will get this file and it will send it out as
an attachment.
Now, if this file was inside the folder, let's say you have a folder here and you have that file inside
that folder, then you have to provide the path, such as name of of the folder, whatever it is.
Let's say it's sample.
And then a slash and then the name of the file.
This is not the case here, so that's OK.
Email was sent.
So go to the browser and here is the email.
I can download it as a file to see the contents because this doesn't, this interface doesn't show the
attached files.
So if you want, you can try another email address that
supports email attachments, so you could do the same Gmail accounts, put it there and send.
And here is the email with text.txt attached as an attachment and that's the contents of that
text file.
And that was how to send an email with an attachment via Python.
Thank you.