I'm trying to write a simple app to send (and possibly receive) emails from my gmail account. I managed to do it while hardcoding my account information in my source code, but now I wanted to enter them in GUI fields and read information from there. Here is the code:
我试着写一个简单的应用程序,从我的gmail账户发送(并可能收到)电子邮件。我设法在我的源代码中硬编码我的帐户信息,但现在我想在GUI字段中输入它们,并从那里读取信息。这是代码:
import sys
import smtplib
from PyQt4 import QtCore, QtGui
from Notifier_Main import Ui_Notifier_Main_GUI
class MainGUI(QtGui.QWidget, Ui_Notifier_Main_GUI):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.sendButton.clicked.connect(self.send)
def send(self):
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username, 'password')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_gui = MainGUI()
main_gui.show()
sys.exit(app.exec_())
When I run it I get this long ass error:
当我运行它时,我得到了一个很长的错误:
C:\Python27\python.exe "E:/Python Projekti/Notifier/src/main.py"
Traceback (most recent call last):
File "E:/Python Projekti/Notifier/src/main.py", line 20, in send
server.sendmail(fromaddr, toaddrs, msg)
File "C:\Python27\lib\smtplib.py", line 728, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "C:\Python27\lib\smtplib.py", line 480, in mail
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
File "C:\Python27\lib\smtplib.py", line 141, in quoteaddr
m = email.utils.parseaddr(addr)[1]
File "C:\Python27\lib\email\utils.py", line 214, in parseaddr
addrs = _AddressList(addr).addresslist
File "C:\Python27\lib\email\_parseaddr.py", line 457, in __init__
self.addresslist = self.getaddrlist()
File "C:\Python27\lib\email\_parseaddr.py", line 218, in getaddrlist
ad = self.getaddress()
File "C:\Python27\lib\email\_parseaddr.py", line 228, in getaddress
self.gotonext()
File "C:\Python27\lib\email\_parseaddr.py", line 204, in gotonext
if self.field[self.pos] in self.LWS + '\n\r':
TypeError: 'in <string>' requires string as left operand, not QString
I tried googling that type error, and found some link about some spyderlib but since I'm pretty new at all this, I couldn't figure out what to do with that.
我试着在google上搜索那个类型的错误,发现了一些关于spyderlib的链接,但是因为我是新手,我不知道该怎么做。
1 个解决方案
#1
3
Most requests to Qt elements that have text will return QStrings, the simple string container Qt uses. Most other libraries are going to expect regular python strings, so casting using str() may be necessary. All of:
对于具有文本的Qt元素的大多数请求将返回qstring,即简单的字符串容器Qt使用。大多数其他库都希望使用常规的python字符串,因此使用str()可能是必要的。所有的:
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
are QString objects.
QString对象。
#1
3
Most requests to Qt elements that have text will return QStrings, the simple string container Qt uses. Most other libraries are going to expect regular python strings, so casting using str() may be necessary. All of:
对于具有文本的Qt元素的大多数请求将返回qstring,即简单的字符串容器Qt使用。大多数其他库都希望使用常规的python字符串,因此使用str()可能是必要的。所有的:
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
are QString objects.
QString对象。