1
00:00:00,330 --> 00:00:07,980
In this video, you will learn how to send out an email that has an attachment with Python.

2
00:00:08,760 --> 00:00:10,290
Let's see how we can do that.

3
00:00:11,250 --> 00:00:12,750
For that, I'm going to fork

4
00:00:14,190 --> 00:00:17,820
perhaps the Send Single Email report.

5
00:00:20,270 --> 00:00:24,200
Since Email with Attachment.

6
00:00:27,780 --> 00:00:29,760
So let's do some things first.

7
00:00:30,030 --> 00:00:36,390
Set a password, a key for the Gmail account, a value.

8
00:00:37,170 --> 00:00:38,520
That's my app passwords.

9
00:00:42,120 --> 00:00:50,400
And press on add new secrets, then the next thing we want to do is let's create a sample file to attach

10
00:00:50,400 --> 00:00:54,210
to our email, text.txt.

11
00:00:56,230 --> 00:00:58,210
Just some content.

12
00:00:59,350 --> 00:00:59,740
That's it.

13
00:01:01,210 --> 00:01:03,850
Text.txt is the file we want to attach.

14
00:01:04,750 --> 00:01:06,700
And then the receiver.

15
00:01:07,240 --> 00:01:10,060
So we're going to send a single email at this time.

16
00:01:10,450 --> 00:01:12,930
The receiver is going to be this one in here.

17
00:01:13,750 --> 00:01:16,270
So on dropmail.me.

18
00:01:17,140 --> 00:01:21,850
You can have disposable email accounts, paste it there.

19
00:01:22,420 --> 00:01:27,040
Sending an email with an attachment is easier than you think.

20
00:01:28,030 --> 00:01:32,290
All you have to do is modify the contents variable.

21
00:01:33,010 --> 00:01:41,980
So, what we have to do is we need to convert this string into a list or to see it more

22
00:01:41,980 --> 00:01:42,600
accurately,

23
00:01:42,610 --> 00:01:46,900
you need to put to this string inside a list like that.

24
00:01:47,470 --> 00:01:52,180
Now we have a list containing one string, which is this one.

25
00:01:53,020 --> 00:02:05,350
But if we add one more item to this list, which could be like text.txt, so the path to this

26
00:02:05,350 --> 00:02:09,610
file and then we send out the email.

27
00:02:11,070 --> 00:02:19,080
Then Python or more specifically, yagmail, this library will get this file and it will send it out as

28
00:02:19,080 --> 00:02:19,830
an attachment.

29
00:02:21,030 --> 00:02:28,470
Now, if this file was inside the folder, let's say you have a folder here and you have that file inside

30
00:02:28,470 --> 00:02:35,730
that folder, then you have to provide the path, such as name of of the folder, whatever it is.

31
00:02:35,880 --> 00:02:38,280
Let's say it's sample.

32
00:02:38,700 --> 00:02:41,190
And then a slash and then the name of the file.

33
00:02:42,120 --> 00:02:44,640
This is not the case here, so that's OK.

34
00:02:44,970 --> 00:02:46,290
Email was sent.

35
00:02:46,890 --> 00:02:51,960
So go to the browser and here is the email.

36
00:02:54,660 --> 00:03:01,530
I can download it as a file to see the contents because this doesn't, this interface doesn't show the

37
00:03:02,280 --> 00:03:03,450
attached files.

38
00:03:03,960 --> 00:03:07,410
So if you want, you can try another email address that

39
00:03:08,510 --> 00:03:16,610
supports email attachments, so you could do the same Gmail accounts, put it there and send.

40
00:03:18,800 --> 00:03:27,080
And here is the email with text.txt attached as an attachment and that's the contents of that

41
00:03:27,080 --> 00:03:27,740
text file.

42
00:03:30,260 --> 00:03:34,340
And that was how to send an email with an attachment via Python.

43
00:03:34,490 --> 00:03:34,910
Thank you.

