Skip to content

Instantly share code, notes, and snippets.

@vijayanandrp
Created November 27, 2017 10:09
Show Gist options
  • Save vijayanandrp/9f22fe49d91ed920419d080d106c0a57 to your computer and use it in GitHub Desktop.
Save vijayanandrp/9f22fe49d91ed920419d080d106c0a57 to your computer and use it in GitHub Desktop.
Sample function for sending emails using python - https://informationcorners.com/read-send-emails-python/
# -*- coding: utf-8 -*-
import re
import email
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import ntpath
import nltk
import datetime
def send_email(to=None, cc=None, bcc=None, subject=None, attachments=None,
message=None, html=True):
email_from = smtp_email
email_to = [email_from]
files_to_send = attachments
# email object
msg = MIMEMultipart()
msg["From"] = email_from
if to:
to = list(set(to))
email_to += to
msg["To"] = ', '.join(to)
if cc:
cc = list(set(cc))
email_to += cc
msg["Cc"] = ', '.join(cc)
if bcc:
bcc = list(set(bcc))
email_to += bcc
msg["Bcc"] = ', '.join(bcc)
if subject:
msg["Subject"] = subject
msg.preamble = subject
else:
msg["Subject"] = email_subject
msg.preamble = email_subject
message_type = 'plain'
if html:
message_type = 'html'
if not message:
message = email_message_html
msg.attach(MIMEText(message, message_type))
else:
message = email_message_plain
msg.attach(MIMEText(message, message_type))
if not isinstance(files_to_send, list):
files_to_send = [files_to_send]
if files_to_send:
for file_to_send in files_to_send:
print('Preparing to send file - {}'.format(file_to_send))
content_type, encoding = mimetypes.guess_type(file_to_send)
if content_type is None or encoding is not None:
content_type = "application/octet-stream"
maintype, subtype = content_type.split("/", 1)
if maintype == "text":
with open(file_to_send) as fp:
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
elif maintype == "image":
with open(file_to_send, "rb") as fp:
attachment = MIMEImage(fp.read(), _subtype=subtype)
elif maintype == "audio":
with open(file_to_send, "rb")as fp:
attachment = MIMEAudio(fp.read(), _subtype=subtype)
else:
with open(file_to_send, "rb") as fp:
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=ntpath.basename(file_to_send))
msg.attach(attachment)
try:
smtp_obj = smtplib.SMTP(host=smtp_server, port=smtp_port, timeout=300)
smtp_obj.sendmail(from_addr=email_from, to_addrs=list(set(email_to)), msg=msg.as_string())
log.info("Successfully sent email to {}".format(str(email_to)))
smtp_obj.quit()
return True
except smtplib.SMTPException:
log.error("Error: unable to send email")
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment