发送到邮件的字符串中使用零个与一个换行时的无换行与双换行区别
我正在发送一段字符串到电子邮件中,我希望每句话都单独占一行,像这样:
"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..过程中的必要信息):
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
可能是因为'\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()
0
看起来代码没有问题,你的每一行“Loaded...”之间应该只有一个空行。
可能是你用来查看邮件的邮箱客户端把换行符当成了新段落,自动加上了间距。你可以试着把 \n
替换成 <br>
,看看这样能否得到你想要的单行间距。
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会返回一个包含失败邮件信息的列表,如果有些邮件发送失败。如果所有邮件都发送失败,就会抛出一个异常;如果全部成功发送,那么这个字典就是空的。
如果你提前检查一下这个返回值,会让你省去很多麻烦。