输入 class 后电子邮件功能不起作用
Email function does not work after put in a class
所以基本上我复制并粘贴了一个电子邮件发送 python 有效的脚本。这是代码:
import smtplib
gmail_user = 'email@email.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, whats up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print ('Email sent!')
except:
print ('Something went wrong...')
结果如描述的那样完美:
不幸的是,我将代码放在 class 中作为另一个主函数的函数执行。代码如下:
import smtplib
class EmailSending():
def doneEmail(self):
gmail_user = 'email@email.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, whats up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print('Email sent!')
except:
print('Something went wrong...')
ef = EmailSending()
ef.doneEmail()
唯一更改的代码是它现在位于 class 函数中。
但是当调用它时,它 returns 不同的结果,如下所示
电子邮件仍然发送到我的电子邮件,但其他所有内容都已发送。谁能帮我吗?谢谢
您在包含 From:
等行的开头添加了空格。删除它们:
class EmailSending():
def doneEmail(self):
gmail_user = 'email@email.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, whats up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print('Email sent!')
except:
print('Something went wrong...')
ef = EmailSending()
ef.doneEmail()
所以基本上我复制并粘贴了一个电子邮件发送 python 有效的脚本。这是代码:
import smtplib
gmail_user = 'email@email.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, whats up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print ('Email sent!')
except:
print ('Something went wrong...')
结果如描述的那样完美:
不幸的是,我将代码放在 class 中作为另一个主函数的函数执行。代码如下:
import smtplib
class EmailSending():
def doneEmail(self):
gmail_user = 'email@email.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, whats up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print('Email sent!')
except:
print('Something went wrong...')
ef = EmailSending()
ef.doneEmail()
唯一更改的代码是它现在位于 class 函数中。
但是当调用它时,它 returns 不同的结果,如下所示
电子邮件仍然发送到我的电子邮件,但其他所有内容都已发送。谁能帮我吗?谢谢
您在包含 From:
等行的开头添加了空格。删除它们:
class EmailSending():
def doneEmail(self):
gmail_user = 'email@email.com'
gmail_password = 'P@ssword!'
sent_from = gmail_user
to = ['bill@gmail.com']
subject = 'OMG Super Important Message'
body = 'Hey, whats up?\n\n- You'
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print('Email sent!')
except:
print('Something went wrong...')
ef = EmailSending()
ef.doneEmail()