通过向函数传递要使用的值。还要注意的是,函数定义应该在使用之前进行。
import smtplib
import random
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def mail(food):
email_user = 'me'
email_send = ['no1','no2']
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = ','.join(email_send)
msg['Subject'] = 'food for the week!'
body = food
msg.attach(MIMEText(body,'plain'))
text = msg.as_string()
mail =smtplib.SMTP("smtp.gmail.com", 587)
mail.ehlo()
mail.starttls()
mail.login(email_user,"pwd")
mail.sendmail(email_user,email_send, text)
mail.close()
mail()
#food dictionary
food = random.choice([spaghetti, pizza])
mail(food)