email과 smtplib를 이용
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL
SMTP_PORT = "465"
SMTP_USER = ""
SMTP_PASSWORD = ""
def send_mail(name, addr, subject, contents, attachment=False):
msg = MIMEMultipart("alternative")
text = MIMEText(contents)
msg.attach(text)
if attachment:
from email.mime.base import MIMEBase
from email import encoders
msg = MIMEMultipart('mixed')
try:
file_data = MIMEBase('application', 'octet-steam')
with oepn(attachment, 'rb') as f:
file_contents = f.read()
file_data.set_payload(file_contents)
encoders.encode_base64(file_data)
from os.path import basename
filename = basename(attachment)
fild_data.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(file_data)
except e:
print("Somthing wrong while attach a file")
smtp = SMTP_SSL(SMTP_SERVER, SMTP_PORT)
smtp.login(SMTP_USER, SMTP_PASSWORD)
msg['From'] = name
msg['To'] = addr
msg['Subject'] = subject
smtp.close()
def main():
global SMTP_USER, SMTP_PASSWORD
SMTP_USER = str(input('Enter User ID : '))
SMTP_PASSWORD = str(input('Enter Password : '))
send_mail('TEST MAIL', 'chinst@naver.com', 'TEST mail from Python', 'This is a test mail.\nIs this OK?')
if __name__ == '__main__':
main()
|
댓글 없음:
댓글 쓰기