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