# General Parameters: in strict accordance with the smooth, the actual parameters are assigned to the formal parameters
# def send (name):
# ...
# Send ("Eric")
def sendmail (MAIL_ADDR):#第一步, create function (MAIL_ADDR is formal parameter) step three, mail_addr = {str} ' [email protected] '
Try# This feature captures anomalies and is not yet learned.
Import Smtplib
From Email.mime.textImport Mimetext
From Email.utilsImport formataddr
msg = Mimetext (' Mail content ',' Plain ',' Utf-8 ')
msg[' from '] = FORMATADDR (["Futian",' [email protected] ')
msg[' to '] = FORMATADDR (["Leave",' [email protected] ')
msg[' Subject '] =Topic
Server = Smtplib. SMTP ("Smtp.163.com",25)
Server.login ("[Email protected]","521why,.")
Server.sendmail (' [email protected] ', [Mail_addr,], msg.as_string ())#将收件人地址 ' [email protected] ' change to a variable
Server.quit ()
Except#只要try下面的代码, catch the exception and execute the code inside the except.
#发送失败执行
# return False #以上代码执行失败, returns a false
Return"No"#返回的也可以是字符串
Else
#发送成功执行
# return True #否则, returns a true
Return"Yes"#return返回值给函数调用者sendmail,
# SendMail ()
# ret = sendmail (' [email protected] ') #第二步, call function (' [email protected] ' is the actual parameter)
# ret = SendMail ("[email protected]")
# Print (ret)
# if Ret = = True: #通过判断ret是False还是True来显示发送状态
# # if Ret = = "CC": #也可以判断返回的是不是定义的字符串值
# print ("Send Success")
# Else:
# print ("Send Failed")
while True:
em = input ( "Please enter your email address:")
# sendmail (EM)
result = SendMail (em) #em是参数传递的内容, can be multiple
# Print (result)
if result = = " Yes ":
print (" sent successfully ")
else:
print ( "send Failed")
Python path "third": Python Basics (13)--function general parameters