Last active
April 19, 2024 12:42
-
-
Save bcschwimm/74d537d843b3c53a8c55a115771b95c9 to your computer and use it in GitHub Desktop.
Tutorial on Sending an email using Python with Subject & BCC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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