#100DaysOfCode in Python Transcripts
Chapter: Days 64-66: Sending emails with smtplib
Lecture: Emailing with BCC

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One last thing I wanted to show you really really quickly,
0:03 was the BCC, okay?
0:06 A lot of the times, if you send, if you're automating
0:08 this sort of thing, you don't want everyone to see
0:10 everyone who this e-mail is being sent to, right?
0:13 So imagine you have a mailing list of something.
0:16 Imagine sending out all 100 to 2,000 e-mails
0:21 and everyone's seeing each other on the e-mail.
0:23 Not great, okay?
0:25 So you can actually BCC this.
0:29 As I mentioned before, MIME does not honor BCC by default,
0:35 it's actually by design.
0:36 Go figure, right?
0:37 If you were to create
0:39 an object in here, message...
0:42 BCC, for that field, it actually works in that,
0:46 that field is populated with the e-mail addresses
0:50 that you put in there.
0:51 The problem is, it doesn't honor the nature of BCC
0:54 in that it's blind.
0:56 It may as well be a CC field,
1:00 tagged as a BCC field.
1:02 It's kind of crazy, right?
1:03 And it was something I struggled with for,
1:05 for a while.
1:06 So the way I get around this using MIME and
1:09 SMTPLib is by throwing it into
1:13 your SMTP server line.
1:16 This send mail line I should say.
1:18 Okay?
1:19 So let's say...
1:22 I have three e-mail addresses.
1:23 I'm just going to copy them and paste them in here, right?
1:29 Done.
1:31 So we've got Codewright's blog, we got my e-mail at Gmail,
1:34 and which doesn't actually work,
1:36 and we've got e-mail at Gmail, which I'm hoping
1:38 doesn't work either.
1:39 Now...
1:41 If we again, if we were to do it this way,
1:44 with the message building in the multi part,
1:47 all of these e-mails would see each other when they get
1:50 your e-mail, right?
1:52 So what we actually want to do,
1:55 is rather than do it in specified
1:58 in your header information,
2:00 because that gets displayed by default by design.
2:04 We'll just throw it down here
2:06 into the send mail section.
2:10 In order to do this though,
2:12 if you think of it this way.
2:14 Think about your types here.
2:17 Your Python types.
2:19 to_address is a string,
2:23 which means you can't easily
2:26 add these onto it, because BCC is a list.
2:31 So how do we get around that?
2:33 Well, we make to_address
2:37 a list down here, and then we add on to it with BCC.
2:42 That's it, okay?
2:44 All you need to do is send this now,
2:46 and everyone on your BCC list
2:51 will get the e-mail and they won't be able
2:53 to see each other.
2:55 And furthermore, in the production environment
2:58 or something more official than this demo,
3:00 you'll probably make your BCC list of e-mails.
3:04 This will be an e-mail list.
3:05 You'll pull from a database or something like that,
3:07 and then you'll just pull in the list, all right?
3:10 You won't have to type them all into your script,
3:13 because that'd be ridiculous.
3:14 So there's our send mail with from_address
3:18 to_address, as a list.
3:21 With BCC added on to it, and then our text body,
3:26 and that's it.
3:27 So enjoy, and good luck with all your
3:30 e-mail automated needs.