python发送邮件简单示例

def send_email(receiver,subject,msg):
    ##receiver 收件人邮箱
    ##subject 邮件标题
    ##msg 邮件内容(支持html代码)

    import smtplib ##导入需要的module
    from email.mime.text import MIMEText
    
    sender = "发件人姓名"
    smptserver = "smtp.gmail.com"
    port = 587
    username = "[email protected]"
    password = "123456"
    msg = MIMEText(msg,'html','utf-8')

    msg['Subject'] = subject

    smtp = smtplib.SMTP(smptserver,port)
    smtp.ehlo()
    smtp.starttls()

    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()



不需要帐号密码,本地为邮件服务器

    def send_email(receiver,subject,msg):

        import smtplib
        from email.mime.text import MIMEText
        sender = "发件人姓名"  
        smptserver = "localhost"
        port = 25
        msg = MIMEText(msg,'html','utf-8')

        msg['Subject'] = subject

        smtp = smtplib.SMTP(smptserver,port) 

        smtp.sendmail(sender, receiver, msg.as_string())
        smtp.quit()


你可能感兴趣的:(python发送邮件简单示例)