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


Talk Python's Mastodon Michael Kennedy's Mastodon