Skip to content

Instantly share code, notes, and snippets.

@juice500ml
Last active July 27, 2016 18:16
Show Gist options
  • Save juice500ml/38c2acd539b318a221667742ea146e3e to your computer and use it in GitHub Desktop.
Save juice500ml/38c2acd539b318a221667742ea146e3e to your computer and use it in GitHub Desktop.
python2 smtp mail sender func
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
from email import Utils
from email.header import Header
import os
import base64
smtp_server = "smtp.daum.net"
port = 465
userid = "USER ID FOR SMTP SERVER"
passwd = "USER PW FOR SMTP SERVER"
def send_mail(from_user, to_user, subject, text, attach):
COMMASPACE = ", "
msg = MIMEMultipart("alternative")
msg["From"] = from_user
msg["To"] = to_user
msg["Subject"] = Header(s=subject, charset="utf-8")
msg["Date"] = Utils.formatdate(localtime = 1)
msg.attach(MIMEText(text, "html", _charset="utf-8"))
if (attach != None):
part = MIMEBase("application", "octet-stream")
part.set_payload(open(attach, "rb").read())
Encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename=\"%s\"" % os.path.basename(attach))
msg.attach(part)
smtp = smtplib.SMTP_SSL(smtp_server, port)
smtp.ehlo()
smtp.login(userid, passwd)
smtp.sendmail(from_user, to_user, msg.as_string())
smtp.close()
# Example
send_mail('juice500ml@hanmail.net', 'juice500ml@gmail.com', "Test Subject 테스트 입력 ", "Test text 테스트 입력 ", "/home/juice500/Desktop/gen.py")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment