简体   繁体   English

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

[英]no newline vs. double newline when using zero vs. one newline in a string sent to email

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): 我发现这可以产生格式正确的电子邮件(但不包括来自..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"

There does not appear to be an issue with the code, there should only be one newline between each of your 'Loaded...' lines. 代码似乎没有问题,每个“ Loaded ...”行之间应该只有一个换行符。

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. 尝试用<br>替换\\n ,看看是否会导致您期望的单个间距。

I ran into the same problem using the smtplib by itself. 我自己使用smtplib遇到了同样的问题。 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. 如果某些电子邮件发送失败,则smtp.sendmail将返回它并带有失败元组列表。 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. 如果您进行检查,将可以避免悲伤。

It may be that the '\\n' should come at the end of a line. 可能'\\ n'应该出现在一行的末尾。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM