当在 python 中使用 smtp 发送电子邮件时,我的 \t 被替换为单个空格。

发布于 2024-12-17 19:54:34 字数 397 浏览 0 评论 0原文

我需要能够在发送的电子邮件中添加选项卡,以便可以将内容复制粘贴到 Excel 中。代码:

SUBJECT = subj
TO = [whoto]
FROM = whofrom
BODY = string.join((
    "From: %s" % FROM,
    "To: %s" % ", ".join(TO),
    "Subject: %s" % SUBJECT ,
    "",
    text
    ), "\r\n")

server = smtplib.SMTP(host)
server.login(log,pass)
server.sendmail(FROM, TO, BODY)
server.quit()

我的文本中有选项卡,但发送电子邮件时没有选项卡。我怎样才能保留标签。

I need to be able to have tabs in the email i send so the content can be copy pasted into excel. Code:

SUBJECT = subj
TO = [whoto]
FROM = whofrom
BODY = string.join((
    "From: %s" % FROM,
    "To: %s" % ", ".join(TO),
    "Subject: %s" % SUBJECT ,
    "",
    text
    ), "\r\n")

server = smtplib.SMTP(host)
server.login(log,pass)
server.sendmail(FROM, TO, BODY)
server.quit()

My text has tabs in it but when the email is sent there are no tabs. How can i get the tabs to remain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

转角预定愛 2024-12-24 19:54:34

我建议使用 mime 编码器库:

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import smtplib
import datetime
import logging

class mailer:
    def __init__(self,SERVER="my.mail.server",FROM="[email protected]"):
        self.server = SERVER
        self.send_from = FROM
        self.logger = logging.getLogger('mailer')

    def send_mail(self, send_to, subject, text, files=[]):
        assert type(send_to)==list
        assert type(files)==list
        if self.logger.isEnabledFor(logging.DEBUG):
            self.logger.debug(' '.join(("Sending email to:",' '.join(send_to))))
            self.logger.debug(' '.join(("Subject:",subject)))
            self.logger.debug(' '.join(("Text:",text)))
            self.logger.debug(' '.join(("Files:",' '.join(files))))
        msg = MIMEMultipart()
        msg['From'] = self.send_from
        msg['To'] = COMMASPACE.join(send_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject
        msg.attach( MIMEText(text) )
        for f in files:
            part = MIMEBase('application', "octet-stream")
            part.set_payload( open(f,"rb").read() )
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
            msg.attach(part)
        smtp = smtplib.SMTP(self.server)
        mydict = smtp.sendmail(self.send_from, send_to, msg.as_string())
        if self.logger.isEnabledFor(logging.DEBUG):
            self.logger.debug("Email Successfully Sent!")
        smtp.close()
        return mydict

请务必检查返回字典,因为它会让您知道是否只有某些人收到了电子邮件。

I suggest using the mime encoder libraries:

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import smtplib
import datetime
import logging

class mailer:
    def __init__(self,SERVER="my.mail.server",FROM="[email protected]"):
        self.server = SERVER
        self.send_from = FROM
        self.logger = logging.getLogger('mailer')

    def send_mail(self, send_to, subject, text, files=[]):
        assert type(send_to)==list
        assert type(files)==list
        if self.logger.isEnabledFor(logging.DEBUG):
            self.logger.debug(' '.join(("Sending email to:",' '.join(send_to))))
            self.logger.debug(' '.join(("Subject:",subject)))
            self.logger.debug(' '.join(("Text:",text)))
            self.logger.debug(' '.join(("Files:",' '.join(files))))
        msg = MIMEMultipart()
        msg['From'] = self.send_from
        msg['To'] = COMMASPACE.join(send_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject
        msg.attach( MIMEText(text) )
        for f in files:
            part = MIMEBase('application', "octet-stream")
            part.set_payload( open(f,"rb").read() )
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
            msg.attach(part)
        smtp = smtplib.SMTP(self.server)
        mydict = smtp.sendmail(self.send_from, send_to, msg.as_string())
        if self.logger.isEnabledFor(logging.DEBUG):
            self.logger.debug("Email Successfully Sent!")
        smtp.close()
        return mydict

Make sure to check the return dictionary, as it will let you know if only some people received the email.

分开我的手 2024-12-24 19:54:34

您需要将电子邮件切换为 HTML 格式并使用表格。就我而言,Gmail 正在将电子邮件输出从制表符转换为空格,以使其看起来正确。

确认这是你的问题。查看原始电子邮件并尝试将其复制并粘贴到其中。

You will need to switch your email to be HTML and use tables. In my case Gmail is converting the Email output from tabs to spaces to make it look correct.

To confirm this is your issue. Look at the raw email and try to copy and paste that in.

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