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

3 回答 3

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 将返回失败元组列表。如果所有电子邮件都发送失败,则抛出异常,如果都发送成功,则字典为空。

如果您检查它,您将免于悲伤。

于 2011-11-08T18:09:58.443 回答
0

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

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

于 2011-11-08T17:30:47.337 回答
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()
于 2012-01-06T16:01:50.683 回答