Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Created December 14, 2012 15:57
Show Gist options
  • Save Apkawa/4286457 to your computer and use it in GitHub Desktop.
Save Apkawa/4286457 to your computer and use it in GitHub Desktop.
The script for testing mailing email
import os
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEBase import MIMEBase
from email import Encoders
msg = MIMEMultipart('alternative')
msg['Subject'] = "Hello from Mandrill, Python style!"
msg['From'] = "John Doe <[email protected]>" # Your from name and email address
msg['To'] = "[email protected]"
text = "Mandrill speaks plaintext"
part1 = MIMEText(text, 'plain')
html = "<em>Mandrill speaks <strong>HTML</strong></em>"
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
try:
attach_file = os.sys.argv[1]
main_type, sub_type = mimetypes.guess_type(attach_file)[0].split('/')
part = MIMEBase(main_type, sub_type)
part.set_payload(open(attach_file).read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.split(attach_file)[1])
msg.attach(part)
except IndexError:
pass
s = smtplib.SMTP('localhost', 1025)
#s.login('login', 'password')
s.sendmail(msg['From'], msg['To'], msg.as_string())
# for test use smtpd
# python -m smtpd -n -c DebuggingServer localhost:1025
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment