def send_exception_mail(exception):
# noinspection PyBroadException
try:
msg = MIMEMultipart('alternative')
msg['Subject'] = "Exception Occurred on betaPika"
msg['From'] = FROM_MAIL
msg['To'] = TO_MAIL
plain = MIMEText(exception, "plain", "utf-8")
msg.attach(plain)
s = smtplib.SMTP()
s.connect(SERVER, PORT)
s.ehlo()
s.starttls()
s.ehlo()
s.login(FROM_MAIL, FROM_PASSWORD)
s.sendmail(FROM_MAIL, TO_MAIL, msg.as_string())
s.quit()
except:
print "Mail Failed"
python类MIMEText()的实例源码
def process_fill_event(self, fill_event):
"""Extension of abstract base class method. The extension implements the
capability to send email notifications when fill events are received.
"""
# Call super class method to handle the fill event.
super(InteractiveBrokersPortfolio, self).process_fill_event(fill_event)
# Send an email notification containing the details of the fill event.
#
# TODO: Would it be better to make server an instance variable so that we
# don't have to recreate it every time there's a fill event?
if self.gmail_and_password is not None:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(*self.gmail_and_pass)
msg = MIMEText(str(fill_event), "plain", "utf-8")
msg["From"] = msg["To"] = self.gmail_and_pass[0]
msg["Subject"] = "Odin Trade Notification"
server.sendmail(
self.gmail_and_pass[0], self.gmail_and_pass[0], msg.as_string()
)
server.quit()
def run(self, nickname=None, content=None, image=None):
text = content + "<br>" + "<img src={image} />".format(image=image) \
if image is not None else content
msg = MIMEText(text, _subtype="html", _charset="utf-8")
msg["Subject"] = "????"
msg["From"] = nickname + "<" + MAIL_SENDER + ">"
msg["To"] = ",".join(MAIL_RECEIVER_LIST)
try:
server = smtplib.SMTP()
server.connect(MAIL_HOST)
server.starttls()
server.login(MAIL_SENDER, MAIL_PASSWORD)
server.sendmail(MAIL_SENDER, MAIL_RECEIVER_LIST, msg.as_string())
server.close()
except Exception as e:
LOG.error('send mail failed with error: %s' % str(e))
return False
return True