Sending mail from Python using SMTP

后端 未结 13 852
半阙折子戏
半阙折子戏 2020-11-28 17:27

I\'m using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I\'m missing ?

from smtplib import SM         


        
13条回答
  •  误落风尘
    2020-11-28 18:00

    you can do like that

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    
    server = smtplib.SMTP('mail.servername.com', 25)
    server.ehlo()
    server.starttls()
    
    server.login('username', 'password')
    from = 'me@servername.com'
    to = 'mygfriend@servername.com'
    body = 'That A Message For My Girl Friend For tell Him If We will go to eat Something This Nigth'
    subject = 'Invite to A Diner'
    msg = MIMEText(body,'plain','utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = Header(from, 'utf-8')
    msg['To'] = Header(to, 'utf-8')
    message = msg.as_string()
    server.sendmail(from, to, message)
    

提交回复
热议问题