from email.mime.text import MIMEText from email.header import Header from smtplib import SMTP_SSL def send_mail(sender_sina=‘‘,pwd=‘‘,receiver=‘‘,mail_title=‘‘,mail_content=‘‘): # 邮箱smtp服务器 host_server = ‘smtp.sina.com‘ sender_sina_mail = sender_sina+‘@sina.com‘ #ssl登录 smtp = SMTP_SSL(host_server) #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 smtp.set_debuglevel(0) smtp.ehlo(host_server) smtp.login(sender_sina, pwd) msg = MIMEText(mail_content, "plain", ‘utf-8‘) msg["Subject"] = Header(mail_title, ‘utf-8‘) msg["From"] = sender_sina_mail msg["To"] = receiver smtp.sendmail(sender_sina_mail, receiver, msg.as_string()) smtp.quit() send_mail("hequan2011","密码","[email protected]","标题",‘内容‘)
#!/usr/bin/python #coding:utf-8 from email.mime.text import MIMEText from email.header import Header from smtplib import SMTP_SSL import sys def send_mail(sender_sina=‘‘,pwd=‘‘,receiver=‘‘,mail_title=‘‘,mail_content=‘‘): host_server = ‘smtp.sina.com‘ sender_sina_mail = sender_sina+‘@sina.com‘ #ssl登录 smtp = SMTP_SSL(host_server) #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 smtp.set_debuglevel(0) smtp.ehlo(host_server) smtp.login(sender_sina, pwd) try: msg = MIMEText(mail_content, "plain", ‘utf-8‘) msg["Subject"] = Header(mail_title, ‘utf-8‘) msg["From"] = sender_sina_mail msg["To"] = receiver smtp.sendmail(sender_sina_mail, receiver, msg.as_string()) smtp.quit() print("发送成功") return True except Exception as e : print("发送失败:",e) return False if __name__=="__main__": #send_mail("hequan2011","密码","[email protected]","标题",‘内容‘) send_mail("hequan2011", "密码", sys.argv[1], sys.argv[2], sys.argv[3])
时间: 2024-11-29 06:51:48