【问题标题】:Email Error help me with syntax error电子邮件错误帮助我解决语法错误
【发布时间】:2014-03-02 08:10:57
【问题描述】:
import smtplib  
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg.attach(MIMEText(file("P:/Email/test.txt").read()))



sender = 'sender@email.com'  
reciever  = 'reciever@email.com'  
msg = 'Hello'  


# Credentials (if needed)  
username = 'user'  
password = 'pass'  

# The actual mail send  
server = smtplib.SMTP('localhost')  
server.starttls()  
server.login(username,password)  
server.sendmail(sender, reciever, msg)  
server.quit()  

回溯(最近一次调用最后一次):文件“attach2.py”,第 27 行,在
server.sendmail(sender, reciever, msg) 文件“C:\Python33\lib\smtplib.py”,第 775 行,在 sendmail (code, resp) = self.data(msg) 文件“C:\Python33\lib\smtplib.py”,第 516 行,在 数据 q = _quote_periods(msg) 文件“C:\Python33\lib\smtplib.py”,第 167 行,在 quote_periods 返回 re.sub(br'(?m)^.', b'..', bindata) 文件 "C:\Python33\lib\re.py", 第 170 行,在 sub return _compile(pattern, flags).sub(repl, string, count) TypeError 中: 预期的字符串或缓冲区

为什么我会看到此错误消息。我的 python 库文件有问题吗?

【问题讨论】:

  • 你应该添加一个标签来吸引更多的注意力。仅电子邮件标签的关注者就很少。
  • 这是什么语言?
  • 您需要添加语言标签。话虽如此,实际阅读您发布的代码。您已经声明了msg = MIMEMultipart(),并且已经将它用作msg.attach,所以您现在显然不能在第10 行中使用msg = 'Hello' 来引用它。我什至不确定这是什么语言,可以看到那个错误。
  • 我在 Python 3.3 中的代码
  • 欢迎您!我已经删除了我的答案,因为您已经更新了问题并且知道它已经完全改变了。您必须了解这不是一个调试站点。在这种情况下,我建议打开不同的问题而不是编辑同一个问题。这会使回答这个特定问题的人感到困惑,并且得到的答案与该问题不再匹配。

标签: python email smtplib


【解决方案1】:

上一行缺少右括号。

...
msg = MIMEMultipart()
msg.attach(MIMEText(file("P:/Email/test.txt").read())) # line missing a parenthesis

sender = 'whosends@something.com'
...

【讨论】:

  • 我以为我已经解决了。嗯。让我试试 msg.attach(MIMEText(file("P:/Email/test.txt").read()))
  • 还是同样的错误信息。 msg.attach(MIMEText(file("P:/Email/test.txt").read())) NameError: name 'file' is not defined
  • 行得通!但新的错误。回溯(最后一次调用):文件“attach2.py”,第 27 行,在 server.sendmail(sender, reciever, msg) 文件“C:\Python33\lib\smtplib.py”,第 775 行,在sendmail (code, resp) = self.data(msg) File "C:\Python33\lib\smtplib.py", line 516, in data q = _quote_periods(msg) File "C:\Python33\lib\smtplib.py ",第 167 行,在 _quote_periods 返回 re.sub(br'(?m)^\.',b'..',bindata) 文件 "C:\Python33\lib\re.py",第 170 行,在 sub return _compile(pattern, flags).sub(repl, string, count) TypeError: expected string or buffer
  • 段,这很难解析。您可以在原始问题中使用格式发布它吗?
  • 对不起。这个网站太难用了。
【解决方案2】:

您的代码有 2 个错误。这是我的更正

import smtplib  
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg.attach(MIMEText(file("P:/Email/test.txt").read()))



sender = 'sender@email.com'  
reciever  = 'reciever@email.com'  

# ***here don't overwrite the MIMEMultipart() obj***
# msg = 'Hello'  


# Credentials (if needed)  
username = 'user'  
password = 'pass'  

# The actual mail send  
server = smtplib.SMTP('localhost')  
server.starttls()  
server.login(username,password)

# ***here msg.as_string() to convert the MIMEMultipart() obj to a string***
server.sendmail(sender, reciever, msg.as_string())  

server.quit()  

【讨论】:

    【解决方案3】:

    这段代码运行良好

    from email.mime.text import MIMEText
    
    # ...
    
    def send_mail(content):
        message = MIMEText(json.dumps(content))
        message["Subject"] = "Alert!"
        message["From"] = "alert@info.com"
        message["To"] = "abbas.kh@gmail.com"
        try:
    
            with smtplib.SMTP("smtp.mailtrap.io", 2525) as server:
                server.set_debuglevel(1)
                server.ehlo()  # Can be omitted
                server.ehlo()  # Can be omitted
                server.login("3247bc881dc002", "e6961e84f746ee")
                server.sendmail(message["From"] , message["To"], message.as_string())
            print('Sent')
        except smtplib.SMTPServerDisconnected:
            print('Failed to connect to the server. Wrong user/password?')
        except smtplib.SMTPException as e:
            print('SMTP error occurred: ' + str(e))
        except Exception as e:
            print('everything else')
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多