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:
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.
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:
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
are QString objects.