使用gmail通过python发送邮件

发布于 2025-01-02 23:52:35 字数 980 浏览 1 评论 0原文

我尝试过使用 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 技术交流群。

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

发布评论

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

评论(2

神回复 2025-01-09 23:52:35

我不知道问题是什么,但我设法使用 python 中已有的 MIME 发送它

所以这里是有效的代码:

def send_mail(self, message):

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    gmailUser = '[email protected]'
    gmailPassword = 'something'
    recipient = '[email protected]'

    msg = MIMEMultipart()
    msg['From'] = gmailUser
    msg['To'] = recipient
    msg['Subject'] = "Subject"
    msg.attach(MIMEText(message))

    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, gmailPassword)
    mailServer.sendmail(gmailUser, recipient, msg.as_string())
    mailServer.close()

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:

def send_mail(self, message):

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    gmailUser = '[email protected]'
    gmailPassword = 'something'
    recipient = '[email protected]'

    msg = MIMEMultipart()
    msg['From'] = gmailUser
    msg['To'] = recipient
    msg['Subject'] = "Subject"
    msg.attach(MIMEText(message))

    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, gmailPassword)
    mailServer.sendmail(gmailUser, recipient, msg.as_string())
    mailServer.close()
云朵有点甜 2025-01-09 23:52:35

问题很可能是消息的内容似乎需要在地址和消息正文之间添加额外的换行符。无论如何,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文