티스토리 뷰

<주요 기능>

1. SMTP 로그인

2. 메일 전송

  • 있는 경우, 파일 첨부
        mailParam = {
            'Subject': "[BRC23C] cReviewCollector_Naver.UpdateOverview() @ " + bTime.GetTimeString(),
            'BodyMessage': str(overviewDF2),
            'File': fileName
        }
        bMailer.SemdMail(mailParam, "mailAccount@gmail.com")
class cBMailer:
    def __init__(self):
        pass

    def GetSMTP(sef):
        thisSMTP = cConstants.thisSMTP
        thisPort = cConstants.thisPort
        # Encryption : TLS

        emailAddress = cConstants.thisEmailAddress
        emailPassword = cConstants.thisEmailPassword

        smtp = smtplib.SMTP(thisSMTP, thisPort)
        smtp.ehlo() # say Hello
        smtp.starttls() # TLS 사용시 필요
        smtp.login(emailAddress, emailPassword)

        return smtp

    def SendMail(self, mailContents, fileName=None):

        smtp1 = self.GetSMTP()
        emailAddress = cConstants.thisEmailAddress

        receiverAddresses = mailContents['Receivers']
        msg = MIMEMultipart()
        msg['Subject'] = mailContents['Subject']
        bodyText = mailContents['Body']
        msg['To'] = receiverAddresses
        msg.attach(MIMEText(bodyText, 'html'))

        if fileName:
            attachment = open(fileName, 'rb')
            part = MIMEBase('application', 'octet-stream')
            part.set_payload((attachment).read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment', filename=fileName)
            msg.attach(part)

        smtp1.sendmail(emailAddress, receiverAddresses, msg.as_string())
        smtp1.quit()

 

 

반응형
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함