send email to localhost from localhost via python

会有一股神秘感。 提交于 2020-01-05 15:18:11

问题


I'm building and testing a web service on my local machine before i put it in production. I want to test the mail service. I'm using the standard python email and smtplib libraries.

import smtplib
from email.mime.text import MIMEText
fp = open('textfile', 'rb')
msg = MIMEText(fp.read())
fp.close()

me = 'me_email@localhost'
you = 'me_again_email@localhost'
msg['Subject'] = 'The contents of %s' %fp
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

I have not configured sendmail and hence it throws an error. But since I just want to test my web service I am not concerned if sendmail cant send an email right now. My service is designed to pulls some records off db and send them an email. So i want to know if this connection, python taking inputs from db and pushing an email is working. I want to receive the email on localhost sent via the script.


回答1:


An SMTP server needs to be configured for sending emails. Sending emails is not possible unless you configure an SMTP server. More information on using python smtplib can be found in pymotw.com, tutorialspoint.com and Python docs.



来源:https://stackoverflow.com/questions/14951109/send-email-to-localhost-from-localhost-via-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!