Created
September 16, 2019 04:42
-
-
Save NicholasBallard/a0c282b65b4475fa47343880e6f33ec2 to your computer and use it in GitHub Desktop.
This file contains 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.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.base import MIMEBase | |
from email import encoders | |
email_user = 'your_email' | |
email_password = 'your_password' | |
email_send = 'recipient_email' | |
subject = 'subject' | |
msg = MIMEMultipart() | |
msg['From'] = email_user | |
msg['To'] = email_send | |
msg['Subject'] = subject | |
body = 'Hi there, sending this email from Python!' | |
msg.attach(MIMEText(body,'plain')) | |
filename='filename' | |
attachment =open(filename,'rb') | |
part = MIMEBase('application','octet-stream') | |
part.set_payload((attachment).read()) | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition',"attachment; filename= "+filename) | |
msg.attach(part) | |
text = msg.as_string() | |
server = smtplib.SMTP('smtp.gmail.com',587) | |
server.starttls() | |
server.login(email_user,email_password) | |
server.sendmail(email_user,email_send,text) | |
server.quit() |
This file contains 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 os | |
import smtplib | |
import sys | |
from email import encoders | |
from email.mime.base import MIMEBase | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
COMMASPACE = ', ' | |
gmail_password = os.environ['gmail_pass'] | |
def email( | |
subject: str = '', | |
message: str = 'Hi!', | |
attachments: list = None, | |
sender: 'email' = '[email protected]', | |
*recipients,): | |
""" Send an email with optional attachments (binary allowed) using my Gmail account. """ | |
recipients = [*recipients] | |
# Create the enclosing (outer) message | |
outer = MIMEMultipart() | |
outer['Subject'] = subject | |
outer['To'] = COMMASPACE.join(recipients) | |
outer['From'] = sender | |
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' | |
# Add the body of the email | |
email_body = MIMEText(message, 'plain') | |
outer.attach(email_body) | |
# Add the attachments to the message | |
if attachments: | |
for file in attachments: | |
try: | |
with open(file, 'rb') as fp: | |
msg = MIMEBase('application', "octet-stream") | |
msg.set_payload(fp.read()) | |
encoders.encode_base64(msg) | |
msg.add_header('Content-Disposition', 'attachment', | |
filename=os.path.basename(file)) | |
outer.attach(msg) | |
except: | |
print("Unable to open one of the attachments. Error: ", | |
sys.exc_info()[0]) | |
raise | |
composed = outer.as_string() | |
# Send the email | |
try: | |
with smtplib.SMTP('smtp.gmail.com', 587) as s: | |
s.ehlo() | |
s.starttls() | |
s.ehlo() | |
s.login(sender, gmail_password) | |
s.sendmail(sender, recipients, composed) | |
s.close() | |
print("Email sent!") | |
except: | |
print("Unable to send the email. Error: ", sys.exc_info()[0]) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment