Created
January 25, 2015 15:51
-
-
Save sungkwangsong/b3300faf36a574447eae to your computer and use it in GitHub Desktop.
send mail using python
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
#!/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 sys | |
import time | |
smtp_server = "SMTP 서버 주소" | |
port = "SMTP 포트번호" | |
userid = "SMTP 계정" | |
passwd = "SMTP 비밀번호" | |
def send_mail(from_user, to_user, cc_users, subject, text, attach): | |
COMMASPACE = ", " | |
msg = MIMEMultipart("alternative") | |
msg["From"] = from_user | |
msg["To"] = COMMASPACE.join(to_user) | |
msg["Cc"] = COMMASPACE.join(cc_users) | |
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(smtp_server, port) | |
smtp.login(userid, passwd) | |
smtp.sendmail(from_user, cc_users, msg.as_string()) | |
smtp.close() | |
now = time.localtime() | |
now = "%04d-%02d-%02d %02d:%02d:%02d" % (now.tm_year, now.tm_mon, now.tm_mday, | |
now.tm_hour, now.tm_min, now.tm_sec) | |
message = "<h2>알수 없는 IP 접근</h2> [%s] from <font color='red'>%s</font> " % (now, sys.argv[1]) | |
send_mail("발송 이메일주소", ["수신자 이메일주소"], ["공동참조자 이메일주소"], "알수 없는 IP 접근 리포트", message, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment