email_fun.py
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def mail():
ret = 123
try:
msg = MIMEText('the mail contents', 'plain', 'utf-8')
msg['From'] = formataddr(['NameA', 'Email_addr_S'])
msg['To'] = formataddr(['nameB', 'Email_addr_R'])
msg['Subject'] = "Subject"
server = smtplib.SMTP('smtp.126.com', 25)
server.login('Email_addr_S','PSD')
server.sendmail('Email_addr_S',['Email_addr_R',],msg.as_string())
server.quit
except Exception:
ret = 456
return ret
ret = mail()
print(ret)
mail_fun.py
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
class mail():
classSpec = "it is a test class"
def __init__(self,UserName,UserAddr):
self.UserName = UserName
self.UserAddr = UserAddr
def mail():
ret = True
try:
msg = MIMEText('the mail contents', 'plain', 'utf-8')
msg['From'] = formataddr(['NameA', 'Email_addr_S'])
msg['To'] = formataddr(['nameB', 'Email_addr_R'])
msg['Subject'] = "Subject"
server = smtplib.SMTP('smtp.126.com', 25)
server.login('Email_addr_S','PSD')
server.sendmail('Email_addr_S',['Email_addr_R',],msg.as_string())
server.quit
except Exception:
ret = False
return ret
email_call.py
#! /usr/lib/python3.5
import mail_fun
worker1 = mail_fun.mail('xxx', 'xxxx@qq.com')
ret = worker1.Semail()
if ret:
print('sent already')
else:
print('failed to send the email')