使用gmail通过python发送邮件
我尝试过使用 python 从 gmail 发送邮件,效果很好。但问题是,当我使用一种方法创建 Mail 类并从代码中向其发送特定字符串时,它无法发送。
class Mail:
def send_mail(self, msg):
import smtplib
fromaddr = '[email protected]'
toaddrs = '[email protected]'
msg = msg + "something"
print msg
username = 'something'
password = 'something'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
这样它会发送邮件,但邮件中唯一的东西是我添加到字符串中的“某物”,并且 print msg 输出整个字符串加上“某物”。可能是什么问题?
这是现在的全班,它的名字叫
mail = Mail()
mail.send_mail(message)
I've tried sending mail with python from gmail and it works fine. But the problem is when I created the Mail class with one method to whom I send specific string from my code, it can't be send.
class Mail:
def send_mail(self, msg):
import smtplib
fromaddr = '[email protected]'
toaddrs = '[email protected]'
msg = msg + "something"
print msg
username = 'something'
password = 'something'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
This way it sends mail but the only thing in mail is "something" that I added to string, and print msg outputs the whole string plus "something". What could be the problem?
This is the whole class for now, and it's called
mail = Mail()
mail.send_mail(message)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道问题是什么,但我设法使用 python 中已有的 MIME 发送它
所以这里是有效的代码:
I don't know what was the problem, but I managed to send it using MIME that's already in python
So here is the code that works:
问题很可能是消息的内容似乎需要在地址和消息正文之间添加额外的换行符。无论如何,iblazevic 给出的解决方案是一种更具可读性的方法。
The problem is most likely that the content of the message seems to requite an extra line break between the addresses and the body of the message. The solution given by iblazevic is a much more readable way of doing it anyway though.