0

I have written this code

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()

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.

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

Thank you!

3
  • To Google! Python Sending Email using SMTP. Nov 12, 2014 at 0:02
  • I'll go give those two articles a read.
    – Sam
    Nov 12, 2014 at 0:09
  • @Sam, just a FYI for future use of SO, if you think things are working but aren't, share as much as you can. In this case you gave your code which is great, but didn't share the content of content which might have helped point others to your problem faster
    – stoves
    Nov 12, 2014 at 0:14

1 Answer 1

8

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.

I believe that you have to change:

content = "The person's name is: "

To this:

content = "\nThe person's name is: "
2
  • That did it! Thank you so much for all your help. Finicky thing, isn't it?
    – Sam
    Nov 12, 2014 at 0:14
  • Yeah, those issues can make you scratch your head for hours. Nov 12, 2014 at 0:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.