Python发送空白电子邮件 [英] Python sending blank e-mails

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

问题描述

我已经编写了这段代码

import urllib.request
import smtplib
import re

htmlweb = urllib.request.urlopen('url goes here')

htmltext = htmlweb.read().decode('utf-8')

regex = '<h2 itemprop="name">(.+?)</h2>'

regex2 = '<div class="mediaPageName">(.+?)</div>'

pattern = re.compile(regex)

pattern2 = re.compile(regex2)

username = re.findall(pattern, htmltext)

interests = re.findall(pattern2, htmltext)

content = "The person's name is: "

i=0
ii=0

while i<len(username):
    content += username[i]
    i += 1

content += "\nTheir interests are: "

while ii<len(interests):
    content += interests[ii] + ", "
    ii += 1

#-----------------------------------------------

mail = smtplib.SMTP("smtp.gmail.com", 587)

mail.ehlo()

mail.starttls()

mail.login('email here', 'password here')

mail.sendmail('email here', 'here too', content)

mail.close()

但是,当我运行它时,会收到一封无内容的电子邮件. content变量会在控制台上显示我想要的内容,但不会在电子邮件正文中显示.

However, when I run it, I receive a body-less e-mail. The content variable prints out what I would like to the console, but not to the e-mail body.

很抱歉,这很简单;这是我第一次尝试Python.

Sorry if this is trivial; this is my first time trying out Python.

谢谢!

推荐答案

以下问题:

The following question: smtplib sends blank message if the message contain certain characters, points out that you must start the content of the message with a new line or else the content will be part of the message's header and not the message's body.

我相信您必须更改:

content = "The person's name is: "

对此:

content = "\nThe person's name is: "

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

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