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

时间:2011-11-08 17:17:05

标签: python email newline arcpy

我正在给一封电子邮件发送一个字符串,我希望每行一句话如下:

"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"

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我自己使用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将返回一个失败元组列表。如果所有电子邮件都无法发送,则抛出异常,如果它们都成功,则字典为空。

如果你检查一下,你会拯救自己。

答案 2 :(得分:0)

可能是'\ 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()