1
00:00:02,590 --> 00:00:08,740
Hi, welcome back! In this video, I'll show you how to send an email which contains an attachment,

2
00:00:09,100 --> 00:00:10,900
a file attached to the email.

3
00:00:11,200 --> 00:00:14,920
And we're going to send the email from Hotmail or Outlook.

4
00:00:15,640 --> 00:00:19,600
So the code you're looking at is the code we've built in the previous video.

5
00:00:20,110 --> 00:00:30,460
And all we have to do is just add some codes here just before we establish the server connection and

6
00:00:30,460 --> 00:00:32,290
sending the email.

7
00:00:32,710 --> 00:00:35,590
So between that and that code.

8
00:00:36,610 --> 00:00:44,410
So we're not going to touch this code or that one, just add the code that prepares the attachment.

9
00:00:45,670 --> 00:00:51,640
We will start from declaring the path to the file

10
00:00:51,640 --> 00:00:55,090
we want to attach, attachment_path, for example.

11
00:00:55,900 --> 00:01:00,670
Now I don't have any file here, so you need a file in your project directory.

12
00:01:01,090 --> 00:01:04,239
So just upload any file that you have in your computer.

13
00:01:04,420 --> 00:01:05,319
It doesn't matter.

14
00:01:05,590 --> 00:01:14,830
So I'm going to just go to upload file and I have something here tiger.jpeg.

15
00:01:14,830 --> 00:01:17,140
So it's a picture. You can upload any file.

16
00:01:19,170 --> 00:01:24,210
So now this is in my local directory here, in my project root directory.

17
00:01:24,540 --> 00:01:32,170
That means I can point to that file by just writing the name tiger.jpeg, like that.

18
00:01:32,910 --> 00:01:37,350
So the files should be in the same direction with your main.py file.

19
00:01:38,040 --> 00:01:40,410
With this script, then

20
00:01:42,990 --> 00:01:48,120
attachment_file is equal to open

21
00:01:50,020 --> 00:01:51,220
attachment_path

22
00:01:52,980 --> 00:01:54,750
in 'rb' mode.

23
00:01:55,710 --> 00:02:00,300
So what we're doing is we are creating a Python file object.

24
00:02:01,050 --> 00:02:07,950
This function actually returns a file object, so it gets  that path and it creates a file object,

25
00:02:08,130 --> 00:02:10,380
which we're going to need later on.

26
00:02:11,970 --> 00:02:15,090
Next, we're going to create a so-called pay load

27
00:02:16,620 --> 00:02:26,370
which is equal to MIMEBase, which is an object of the MIME protocol, and we need to import that

28
00:02:26,370 --> 00:02:26,940
also.

29
00:02:27,390 --> 00:02:36,510
So go to the import and say from email.mime.base mind import MIMEBase.

30
00:02:37,680 --> 00:02:39,410
So we instantiate that here.

31
00:02:40,530 --> 00:02:42,690
The first argument is application.

32
00:02:42,930 --> 00:02:49,530
So these are some protocols that you need to declare in the form of strings.

33
00:02:49,950 --> 00:02:52,140
Octate-stream is the second one.

34
00:02:53,790 --> 00:03:04,080
And then you point to payload, the variable and then call the set_payload method which expects

35
00:03:04,080 --> 00:03:13,620
as argument the file object, which is attachment file, and actually it expects the contents of that

36
00:03:13,620 --> 00:03:14,610
file object.

37
00:03:14,970 --> 00:03:22,830
So to get the contents you need to use read. The read method extract the contents from that file.

38
00:03:23,190 --> 00:03:29,970
So this basically is the contents of that tiger.jpeg file in binary mode.

39
00:03:32,620 --> 00:03:42,670
Then you also need to add a header to payload using the add_header method which expose the name,

40
00:03:42,670 --> 00:03:51,580
which should be 'Content-disposition', disposition, so be careful with that, content disposition.

41
00:03:52,300 --> 00:04:00,370
The second argument is you want to declare that this is about attachments, and then the last argument of the

42
00:04:00,460 --> 00:04:08,980
add_header method is the filename argument, which is equal to attachment_path.

43
00:04:09,010 --> 00:04:09,610
Why?

44
00:04:09,940 --> 00:04:15,970
Well, because this contains the name of the file you want to attach.

45
00:04:15,970 --> 00:04:24,430
So file name, whatever you place here is going to be used as the name of the file attached in the email.

46
00:04:24,670 --> 00:04:33,430
If you don't have this argument, then Python will set a generic file name for the attached file,

47
00:04:33,580 --> 00:04:37,840
so it makes sense to have the same name as the original name here.

48
00:04:37,840 --> 00:04:43,120
So I'm going to place here attachment file path, and that's it.

49
00:04:43,420 --> 00:04:49,630
Finally, you want to say message.attach and you want to attach to the payload variable.

50
00:04:50,530 --> 00:04:52,810
So that one that we created in here.

51
00:04:52,810 --> 00:04:57,970
And we added the contents of the file and the header to that attachment.

52
00:04:58,990 --> 00:05:00,700
And that's it.

53
00:05:01,810 --> 00:05:02,650
Run the code.

54
00:05:04,740 --> 00:05:06,720
And look at the output.

55
00:05:07,050 --> 00:05:10,470
So this one in here is the content, which is binary.

56
00:05:11,880 --> 00:05:14,130
So this message here

57
00:05:14,130 --> 00:05:17,880
it contains some binary codes because of the jpeg file.

58
00:05:18,060 --> 00:05:21,720
But then we have this Unicode error. To fix this,

59
00:05:22,140 --> 00:05:26,190
You want to import in the imports lines in here,

60
00:05:26,460 --> 00:05:36,780
you want to import from email import encoders and then use that encoders just after you set the

61
00:05:36,780 --> 00:05:37,740
payload.

62
00:05:38,610 --> 00:05:46,860
So between set payload and add header, you want to point to the encoders and then to the encode

63
00:05:46,860 --> 00:05:53,490
base64 method and then enter payload there.

64
00:05:54,630 --> 00:05:59,160
So that will convert the payload into base64 format.

65
00:05:59,820 --> 00:06:04,230
So basically, it will convert the attachment into this base64 format.

66
00:06:04,620 --> 00:06:05,370
So run again.

67
00:06:08,210 --> 00:06:09,440
No errors.

68
00:06:10,250 --> 00:06:11,640
Let's check the email.

69
00:06:12,740 --> 00:06:14,220
You'll have to wait a while.

70
00:06:14,280 --> 00:06:16,790
Yeah, so that's the email.

71
00:06:20,550 --> 00:06:27,510
So hi there. That's the text, so the body of the email and the attachment tiger.jpeg.

72
00:06:29,140 --> 00:06:31,810
So that concludes this video.

73
00:06:31,840 --> 00:06:39,250
This is how you send an email and attach a file to that email using Outlook or Hotmail.

74
00:06:39,490 --> 00:06:42,970
So thank you for following and let's have some more videos.

75
00:06:42,980 --> 00:06:45,460
That's a lot of content in the course, so I'll see you later.

