如何使用 smtplib 和 MIMEText 发送电子邮件?

发布于 2024-11-16 02:25:02 字数 1549 浏览 2 评论 0原文

我花了几天时间寻找答案,直到现在我还没有解决我的问题。 我想要的只是在发生错误时从我的程序发送一封简单的电子邮件。 我正在使用 gmail 服务器来执行此操作,它实际上发送了电子邮件,但电子邮件的消息没有出现。 代码如下:

def accept(self):                    
    HOST = 'smtp.gmail.com'
    PORT = '587'
    user = '***********@gmail.com'
    senha = '***********'
    servidor = smtplib.SMTP()
    servidor.connect(HOST, PORT)
    servidor.ehlo()
    servidor.starttls()
    servidor.ehlo()
    servidor.login(user, senha)
    assunto = 'Relatorio de Erro'
    para = '**************@hotmail.com'
    endereco = self.nemail.text()
    de = str(endereco)
    texto = self.message.toPlainText()
    textos = Message(texto)
    corpo = MIMEText(textos)
    corpo['From'] = de
    corpo['To'] = user
    corpo['Subject'] = assunto        
    servidor.set_debuglevel(1)
    servidor.sendmail(user, para, corpo.as_string())        
    servidor.quit

这会在 MIMEText 上引发错误。说无法将 MIMEText 转换为 str。 我尝试了另一个版本,仅供测试。 发送一个简单的字符串:

def accept(self):                    
    HOST = 'smtp.gmail.com'
    PORT = '587'
    user = '*************@gmail.com'
    senha = '************'
    servidor = smtplib.SMTP()
    servidor.connect(HOST, PORT)
    servidor.ehlo()
    servidor.starttls()
    servidor.ehlo()
    servidor.login(user, senha)
    assunto = 'Relatorio de Erro'
    para = '*********@hotmail.com'        
    servidor.set_debuglevel(1)
    servidor.sendmail(user, para, assunto)        
    servidor.quit()

电子邮件到达正确的收件箱,但没有任何消息,什么也没有。 我搜索了很多,我读了文档,我尝试了很多,以至于我什至不知道我在做什么。看来我快要明白了。 有人可以帮助我吗?

I'm looking for an answer for some days and untill now I just didn't solve my problem.
All I want is to send a simple email message from my program if an error occure.
I'm using the gmail server to do that, and it actually sends the email, but the message of the email doesn't appear.
Here's the code:

def accept(self):                    
    HOST = 'smtp.gmail.com'
    PORT = '587'
    user = '***********@gmail.com'
    senha = '***********'
    servidor = smtplib.SMTP()
    servidor.connect(HOST, PORT)
    servidor.ehlo()
    servidor.starttls()
    servidor.ehlo()
    servidor.login(user, senha)
    assunto = 'Relatorio de Erro'
    para = '**************@hotmail.com'
    endereco = self.nemail.text()
    de = str(endereco)
    texto = self.message.toPlainText()
    textos = Message(texto)
    corpo = MIMEText(textos)
    corpo['From'] = de
    corpo['To'] = user
    corpo['Subject'] = assunto        
    servidor.set_debuglevel(1)
    servidor.sendmail(user, para, corpo.as_string())        
    servidor.quit

This raises an error on the MIMEText. Says that can't convert MIMEText to str.
The I tried another version, just for test.
Sending a simple string:

def accept(self):                    
    HOST = 'smtp.gmail.com'
    PORT = '587'
    user = '*************@gmail.com'
    senha = '************'
    servidor = smtplib.SMTP()
    servidor.connect(HOST, PORT)
    servidor.ehlo()
    servidor.starttls()
    servidor.ehlo()
    servidor.login(user, senha)
    assunto = 'Relatorio de Erro'
    para = '*********@hotmail.com'        
    servidor.set_debuglevel(1)
    servidor.sendmail(user, para, assunto)        
    servidor.quit()

The email comes to the right inbox, but it has no message, nothing.
I searched a lot, I'd read the docs, I tried it so much that I don't even know what I'm doing. It looks like I'm almost getting.
Can someone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海里的那抹蓝 2024-11-23 02:25:02

这是根据我在进行系统维护时使用的代码修改的工作版本。


#!/usr/bin/env python

import smtplib

host = 'smtp.gmail.com'
port = '587'
user = '[email protected]'
passw = 'password'

server = smtplib.SMTP()
server.connect(host, port)
server.ehlo()
server.starttls()
server.login(user, passw)

notice = "Completed"
tolist = ["[email protected]"]
fromaddr = '"IT Staff" '
subject = 'Oracle server maintenance notice'
message = '''

The Oracle database server maintenance is

%s

''' % notice

hdr = "From: %s\r\nTo: %s\r\nSubject: %s\r\nX-Mailer: My-Mail\r\n\r\n" % (fromaddr, tolist, subject)
server.sendmail("[email protected]", tolist, hdr+message)
server.quit

Here is a working version modified from code I use when we do system maintenance.


#!/usr/bin/env python

import smtplib

host = 'smtp.gmail.com'
port = '587'
user = '[email protected]'
passw = 'password'

server = smtplib.SMTP()
server.connect(host, port)
server.ehlo()
server.starttls()
server.login(user, passw)

notice = "Completed"
tolist = ["[email protected]"]
fromaddr = '"IT Staff" '
subject = 'Oracle server maintenance notice'
message = '''

The Oracle database server maintenance is

%s

''' % notice

hdr = "From: %s\r\nTo: %s\r\nSubject: %s\r\nX-Mailer: My-Mail\r\n\r\n" % (fromaddr, tolist, subject)
server.sendmail("[email protected]", tolist, hdr+message)
server.quit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文