Last active
May 12, 2016 21:52
-
-
Save cadmuxe/9ef6a9a5cb3f21a2060fbd993caab04f 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.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.utils import make_msgid | |
from email.utils import formatdate | |
from email.MIMEImage import MIMEImage | |
from os import path | |
def sendmail(): | |
smtpsrv =smtplib.SMTP("smtp.server") | |
mail = MIMEMultipart() | |
mail["From"] = "" | |
mail["To"] = "" # "mail1@gmail.com, mail2@gmail.com" | |
# mail["Cc"] = "mail3@gmail.com, mail4@gmail.com" | |
mail["Subject"] = "subject" | |
mail["Message-Id"] = make_msgid() | |
mail["Date"] = formatdate(localtime=True) | |
# attachment | |
filename = "file path" | |
f = open(filename) | |
attachment = MIMEApplication(f.read()) | |
attachment.add_header("Content-Disposition", 'attachment; filename="%s"' % path.basename(filename)) | |
mail.attach(attachment) | |
# html | |
msgAlternative = MIMEMultipart('alternative') | |
mail.attach(msgAlternative) | |
msgText = MIMEText("This is the alternative plain text message.") | |
msgAlternative.attach(msgText) | |
msgText = MIMEText(""" | |
<h1>Hi</h1> | |
<p> | |
<h2>pic</h2> | |
<img src="cid:image1" width="400" height="300"> | |
</p> | |
""", 'html') | |
msgAlternative.attach(msgText) | |
with open("pic path", 'rb') as f: | |
msgImage = MIMEImage(f.read()) | |
msgImage.add_header('Content-ID', '<image1>') | |
mail.attach(msgImage) | |
toaddrs = mail["To"].split(",") | |
# toaddrs += ["cc email addr1", "email addr2"] | |
smtpsrv.sendmail(mail["From"], toaddrs, mail.as_string()) | |
smtpsrv.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment