1、初识函数
函数,可以当作是一大堆功能代码的集合
def 函数名():
函数内编写的代码
...
...
函数名()
举个例子
# 定义名称为info的函数
def info():
print('第一个')
print('第二个')
print('第三个')
info() ## 执行函数内的代码
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
mail_host = 'XXX'
mail_user = 'XXX'
mail_pass = 'XXX'
sender = 'XXX'
receivers = sender
msg_string = 'TEXT邮件'
msg_html = '<p>Python 发送邮件测试</p>'
message = MIMEText(msg_html,'html','utf-8')
message['Subject'] = '测试主题'
message['From'] = '发件人邮箱地址'
message['To'] = '收件人邮箱地址'
try:
smtp_sh = smtplib.SMTP()
smtp_sh.connect(mail_host,25)
smtp_sh.login(mail_user,mail_pass)
smtp_sh.sendmail(sender,receivers,message.as_string())
smtp_sh.quit()
print('send success')
except smtplib.SMTPException as e:
print('send error',e)
这篇博客介绍了Python中函数的基本概念,并通过一个实例展示了如何定义和调用函数。此外,还详细演示了使用SMTP库发送邮件的过程,包括设置邮件内容、发送者、接收者以及处理SMTP异常情况。
1584

被折叠的 条评论
为什么被折叠?



