python_apps/automations/sendmail.py
2023-11-03 14:49:12 +09:00

53 lines
1.9 KiB
Python

import smtplib, os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
class sendmail:
def __init__(self,recipient,title,contents,attachment):
self.sender = 'conteenew@gmail.com'
self.smtpserver = 'smtp.gmail.com'
self.smtpserverport = 587
self.serverpass = 'gzfscocayuutidxo'
self.recipient = recipient
self.title = title
self.contents = contents
self.attachment = attachment
self.cc = 'maddie@conteenew.com'
def smtprun(self):
smtp = smtplib.SMTP(self.smtpserver, self.smtpserverport)
smtp.ehlo()
smtp.starttls()
smtp.login(self.sender, self.serverpass)
smtp.sendmail(self.sender, self.recipient, self.message().as_string())
def message(self):
msg = MIMEMultipart()
msg['From'] = self.sender
msg['To'] = self.recipient
msg['Subject'] = self.title
msg['Cc'] = self.cc
msg.attach(MIMEText(self.contents, 'plain'))
attlist = self.attachment.split(",")
for i in range(len(attlist)):
attached_file = open(attlist[i], 'rb').read()
att = MIMEBase('application', "octet-stream")
att.set_payload(attached_file)
encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment', filename=os.path.basename(self.attachment))
msg.attach(att)
return msg
#if __name__ == '__main__':
# #사용법
# to = 'maddiekorea@gmail.com'
# title = "Test"
# contents = """6...7"""
# #첨부는 쉼표로 여러개 넣을 수 있다.
# attachments = "/Users/maddiekorea/PycharmProjects/automations/플레이오토_주문취합_다운로드파일_필드전체적용_샘플.xlsx"
#
# mail = sendmail(to,title,contents,attachments)
# mail.smtprun()