IT/Python

Python E-Mail Send

Bigtrue 2021. 1. 13. 14:57

Python E-Mail Send

To_User = 'ToUser@email.com'
From_User = 'SendUser@email.com'
Msg_Subject = 'Subject'
Msg_Description = 'Description'

serverName = 'imail2.targetServerName.com'

msg = MIMEMultipart()

# msg = MIMEText(Msg_Description)       ## 본문
msg['To'] = To_User
msg['From'] = From_User
msg['Subject'] = Msg_Subject      ##제목
msg.attach(MIMEText(Msg_Description))
# msgA.attach(msg)
# msg['Bcc'] = email.utils.formataddr(('Recipient', To_User))  # Recommended for mass emails

path = r'C:\\@@@@.xlsx'
part = MIMEBase("application",'octet-stream')
part.set_payload(open(path,'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'% os.path.basename(path))
msg.attach(part)

server = smtplib.SMTP(serverName)
server.sendmail(From_User, To_User, msg.as_string())
server.quit()
print(To_User + ' Email sent successfully')