在发送到电子邮件的字符串中使用零与一个换行符时,无换行符与双换行符

发布于 2024-12-14 03:12:00 字数 1733 浏览 4 评论 0原文

我正在向电子邮件发送一个字符串,并且我想要每行一个句子,如下所示:

"Loaded LLARY_AR with 0 features
Loaded LLARY_LN with 44 features
Loaded LLARY_PT with 23 features"

但是,当我向字符串连接中添加一个换行符时,我会得到两个换行符而不是一个,如下所示:

"Loaded LLARY_AR with 0 features

Loaded LLARY_LN with 44 features

Loaded LLARY_PT with 23 features"

如果我不包含换行符我得到这个:

"Loaded LLARY_AR with 0 features Loaded LLARY_LN with 44 features Loaded LLARY_PT with 23 features"

这是代码:

msgemail = ""
for fcl in trnlist:
    try:
        tofc = param["tsde"]+"\\"+param["trema"]+fcl
        fromfc = param["msde"]+"\\"+param["mchema"]+fcl
        arcpy.DeleteFeatures_management(tofc)
        arcpy.Append_management(fromfc, tofc)
        msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
        del fcl, tofc, fromfc
    except:
        msgemail +="\nUnsuccessful!! "+fcl

emailto = ["email@to","email@to"]
server = smtplib.SMTP('server.here')
header = "Results:\n"
subject = "Data"
mailmessage = header+msgemail+"\n\nCheck log for details"
message = 'Subject: %s\n\n%s' %(subject, mailmessage)
for e in emailto:
    try:
        server.sendmail("email@from",e, message)
    except:
        arcpy.AddMessage(e+" was not sent an email.")
server.quit()

我不明白为什么换行符以这种方式运行..并且是新手..显然这里缺少一些东西。

我发现这可以生成一封格式良好的电子邮件(但不包含 ..GetCount..process 中的必要信息):

msgemail +="\nLoaded"+fcl

虽然这些不会生成格式良好的电子邮件:

msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
msgemail +="\nLoaded "+fromcnt
msgemail +="\nLoaded "+fromcnt+" testing for string at end"

I'm sending a string to an email, and I want one sentence per line like this:

"Loaded LLARY_AR with 0 features
Loaded LLARY_LN with 44 features
Loaded LLARY_PT with 23 features"

But when I add one newline to the string concatenation I get two newlines instead of one, like this:

"Loaded LLARY_AR with 0 features

Loaded LLARY_LN with 44 features

Loaded LLARY_PT with 23 features"

And if I do not include a newline I get this:

"Loaded LLARY_AR with 0 features Loaded LLARY_LN with 44 features Loaded LLARY_PT with 23 features"

Here's the code:

msgemail = ""
for fcl in trnlist:
    try:
        tofc = param["tsde"]+"\\"+param["trema"]+fcl
        fromfc = param["msde"]+"\\"+param["mchema"]+fcl
        arcpy.DeleteFeatures_management(tofc)
        arcpy.Append_management(fromfc, tofc)
        msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
        del fcl, tofc, fromfc
    except:
        msgemail +="\nUnsuccessful!! "+fcl

emailto = ["email@to","email@to"]
server = smtplib.SMTP('server.here')
header = "Results:\n"
subject = "Data"
mailmessage = header+msgemail+"\n\nCheck log for details"
message = 'Subject: %s\n\n%s' %(subject, mailmessage)
for e in emailto:
    try:
        server.sendmail("email@from",e, message)
    except:
        arcpy.AddMessage(e+" was not sent an email.")
server.quit()

I don't understand why the newline acts in this manner..and a newbie..obviously missing something here.

I found that this works to produce an email that is nicely formated (but does not include the necessary information from the ..GetCount..process):

msgemail +="\nLoaded"+fcl

While these do not result in a nicely formated email:

msgemail +="\nLoaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features"
msgemail +="\nLoaded "+fromcnt
msgemail +="\nLoaded "+fromcnt+" testing for string at end"

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

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

发布评论

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

评论(3

や莫失莫忘 2024-12-21 03:12:00

代码似乎没有问题,每一行“已加载...”之间应该只有一个换行符。

您用来查看电子邮件的电子邮件客户端可能会将换行符解释为新段落并自动插入该空格。尝试将 \n 替换为
并查看是否会产生您期望的单间距。

There does not appear to be an issue with the code, there should only be one newline between each of your 'Loaded...' lines.

It is possible that the email client you are using to view the email is interpreting newlines as new paragraphs and inserting that spacing automatically. Try replacing the \n with <br> and see if that results in the single spacing you expect.

分開簡單 2024-12-21 03:12:00

我单独使用 smtplib 遇到了同样的问题。我发现更好的方法是这样的:

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

def send_mail(send_from, send_to, subject, text, files=[], server='mail.server.com'):
    assert type(send_to)==list
    assert type(files)==list
    msg = MIMEMultipart()
    msg['From'] = 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(server)
    mydict = smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()
    return mydict

注意字典的返回!如果某些电子邮件发送失败,smtp.sendmail 将返回失败元组列表。如果所有电子邮件发送失败,则抛出异常,如果全部成功,则字典为空。

如果你检查一下,你就会避免悲伤。

I ran into the same problem using the smtplib by itself. I found that a better way to do it is this:

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

def send_mail(send_from, send_to, subject, text, files=[], server='mail.server.com'):
    assert type(send_to)==list
    assert type(files)==list
    msg = MIMEMultipart()
    msg['From'] = 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(server)
    mydict = smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()
    return mydict

Note the return of the dictionary! The smtp.sendmail will return it with a list of failure tuples if some emails fail to send. If all emails fail to send, an exception is thrown, and if they all succeed, then the dictionary is empty.

You will save yourself grief if you check it.

岁月苍老的讽刺 2024-12-21 03:12:00

'\n' 可能应该出现在行尾。

这听起来很愚蠢,可能行不通,但可能值得一试。

msgemail = "\n"
for fcl in trnlist:
    try:
        tofc = param["tsde"]+"\\"+param["trema"]+fcl
        fromfc = param["msde"]+"\\"+param["mchema"]+fcl
        arcpy.DeleteFeatures_management(tofc)
        arcpy.Append_management(fromfc, tofc)
        msgemail +="Loaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features\n"
        del fcl, tofc, fromfc
    except:
        msgemail +="\nUnsuccessful!! "+fcl

emailto = ["email@to","email@to"]
server = smtplib.SMTP('server.here')
header = "Results:\n"
subject = "Data"
mailmessage = header+msgemail+"\n\nCheck log for details"
message = 'Subject: %s\n\n%s' %(subject, mailmessage)
for e in emailto:
    try:
        server.sendmail("email@from",e, message)
    except:
        arcpy.AddMessage(e+" was not sent an email.")
server.quit()

It may be that the '\n' should come at the end of a line.

It sounds dumb and may not work, but it might be worth a shot.

msgemail = "\n"
for fcl in trnlist:
    try:
        tofc = param["tsde"]+"\\"+param["trema"]+fcl
        fromfc = param["msde"]+"\\"+param["mchema"]+fcl
        arcpy.DeleteFeatures_management(tofc)
        arcpy.Append_management(fromfc, tofc)
        msgemail +="Loaded "+fcl+" with "+str(arcpy.GetCount_management(fromfc))+" features\n"
        del fcl, tofc, fromfc
    except:
        msgemail +="\nUnsuccessful!! "+fcl

emailto = ["email@to","email@to"]
server = smtplib.SMTP('server.here')
header = "Results:\n"
subject = "Data"
mailmessage = header+msgemail+"\n\nCheck log for details"
message = 'Subject: %s\n\n%s' %(subject, mailmessage)
for e in emailto:
    try:
        server.sendmail("email@from",e, message)
    except:
        arcpy.AddMessage(e+" was not sent an email.")
server.quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文