收到一封空白电子邮件 [英] getting a blank email

查看:133
本文介绍了收到一封空白电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经快速编写了以下代码,以将数据发送到REST远程服务器进行调试,但是我收到的是空白电子邮件.现在确定出了什么问题.在终端正文中,将打印出dict文本或json转换后的文本,但在电子邮件中却什么也没得到.

I have quickly written below code to send data being sent to REST remote server for debuging but I am receiving blank email. now sure what is going wrong . in the terminal body dict text or json converted text is getting printed but getting nothing in email.

# For testing
def sendMail(usr, pwd, to, body):
    """ just for testing to send error via email
    """
    fromaddr = usr
    toaddr = to
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "Data add request"
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(usr, pwd)
        msg.attach(body)
        r = json.dumps(body)
        loaded_r = json.loads(r)
        print "json: ", str(loaded_r)
        server.sendmail("error-report@do_not_reply.com", to,  str(loaded_r))
    except Exception, err:
        print 'Error sending email: ', err
    finally:
        server.quit()

我尝试发送 server.sendmail("error-report@do_not_reply.com",到正文),但这也发送了没有主题的空白电子邮件.我在做什么错了?

I tried sending server.sendmail("error-report@do_not_reply.com", to, body) but that too sends blank email without subject. what am I doing wrong ?

推荐答案

您作为 sendmail 的第三个参数传递的消息必须是有效的,格式正确的RFC822消息.根据定义,JSON文件不是有效的电子邮件.

The message you pass as the third argument to sendmail needs to be a valid, properly formatted RFC822 message. A JSON file is, by definition, not a valid email message.

def sendMail(usr, pwd, to, body):
    msg = MIMEText(body)
    msg['From'] = usr
    msg['To'] = to
    msg['Subject'] = "Data add request"
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(usr, pwd)
        server.send_message("error-report@do_not_reply.com", [to],  msg)
    except Exception, err:
        print 'Error sending email: ', err
    finally:
        server.quit()

我在这里切换到 send_message 是因为它处理了将 email.Message 对象再次转换为字符串对象然后调用 sendmail .

I switched to send_message here because it takes care of the minor mundane detail of converting the email.Message object to a string object again before calling sendmail.

尚不清楚您是否希望主体是显示字符串 body 内容的文本部分,还是包含 body 作为JSON的JSON附件,或者两者

It's not clear if you expect the body to be a text part displaying the contents of the string body, or a JSON attachment containing body as JSON, or perhaps both.

如果只需要一个身体部位,则将消息分为多个部分显然是不必要的.如果需要多个部分,则每个部分都应为单独的 MIMEText 或某些其他MIME容器类型,您可以将其 msg.attach()转换为顶级 MIMEMultipart .

If you only need one body part, making the message multipart is obviously unnecessary. If you want multiple parts, then each of them should be a separate MIMEText or some other MIME container type which you can msg.attach() to a top-level MIMEMultipart.

这篇关于收到一封空白电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆