Skip to content

Instantly share code, notes, and snippets.

@bcschwimm
Last active April 19, 2024 12:42
Show Gist options
  • Save bcschwimm/74d537d843b3c53a8c55a115771b95c9 to your computer and use it in GitHub Desktop.
Save bcschwimm/74d537d843b3c53a8c55a115771b95c9 to your computer and use it in GitHub Desktop.
Tutorial on Sending an email using Python with Subject & BCC
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from_addr = 'yourgmail@gmail.com'
to_addr = 'recipiantmail@gmail.com'
bcc = ['emailone@gmail.com', 'emailtwo@gmail.com']
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'email_subject_here'
body = '''This is the body of your email'''
msg.attach(MIMEText(body, 'plain'))
smtp_server = smtplib.SMTP('smpt.gmail.com', 587)
smtp_server.ehlo()
smtp_server.starttls() # Encrypts Session with TLS
smpt_server.login('yourgmail@gmail.com', 'application id/pw')
text = msg.as_string() # Need to Extract Text from msg object
smtp_server.sendmail(from_addr, [to_addr] + bcc, text) # With No BCC remove the [ ] from to_addr and '+ bcc'
smtp_server.quit()
print('Email Sent Successfully')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment