请帮我!我想在 python 3 中创建一个脚本,将电子邮件发送给任何人,但来自我的本地机器。我现在是 python 的初学者,这就是为什么我尝试过的大多数脚本根本不起作用的原因。如果您还解释我需要与脚本一起做什么,那将是一个很大的帮助。谢谢!
问问题
12906 次
3 回答
5
这个短小的数字怎么样。
'''
Created on Jul 10, 2012
test email message
@author: user352472
'''
from smtplib import SMTP_SSL as SMTP
import logging
import logging.handlers
import sys
from email.mime.text import MIMEText
def send_confirmation():
text = '''
Hello,
Here is your test email.
Cheers!
'''
msg = MIMEText(text, 'plain')
msg['Subject'] = "test email"
me ='yourcooladdress@email.com'
msg['To'] = me
try:
conn = SMTP('smtp.email.com')
conn.set_debuglevel(True)
conn.login('yourcooladdress', 'yoursophisticatedpassword')
try:
conn.sendmail(me, me, msg.as_string())
finally:
conn.close()
except Exception as exc:
logger.error("ERROR!!!")
logger.critical(exc)
sys.exit("Mail failed: {}".format(exc))
if __name__ == "__main__":
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
random_ass_condition = True
if random_ass_condition:
send_confirmation()
于 2012-07-10T08:12:52.700 回答
2
看看以下内容:
它们非常易于使用,应该对基本的电子邮件消息进行分类
于 2012-07-09T14:25:07.840 回答
2
开门见山:
from smtplib import SMTP_SSL as SMTP
from email.mime.text import MIMEText
def send_email(subject, body, toaddr="helloworld@googlegroups.com"):
fromaddr = "johndoe@gmail.com"
msg = MIMEText(body, 'plain')
msg['To'] = toaddr
msg['Subject'] = subject
server = SMTP('smtp.gmail.com')
server.login(fromaddr, "y0ur_p4ssw0rd")
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()
所以你现在可以这样称呼它:
send_email(subject, body)
或者您可以使用第三个参数来指定电子邮件地址:
send_email(subject, body, "yourfriend@gmail.com")
于 2018-09-25T22:09:51.767 回答