1

What I am doing is taking information from a web page and attempting to put it into an e-mail in a format like: First Name: first \n#first is a variable Last Name: last #last is a variable

Below is my code:

import smtplib
import base64

from email.MIMEMultipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('relative')
msg['Subject'] = 'Confirmation E-Mail'
msg['From'] = "none"
msg['To'] = email
text1 = "First Name: ", first_name, "<br>Last Name: ", last_name
part1 = MIMEText(text1, 'html')

s = smtplib.SMTP('localhost')
s.sendmail(email, email, msg.as_string())
s.quit()

first_name and last_name are pulled from the web page!

1
  • Although the question has already been answered I'll just add: The lines that you wrote were "the rest of the errors" are not other errors. There was a single error here resulting in a traceback. You can Google 'python tracebacks' to learn more. When you get a traceback it's best to post the entire thing starting with the word "Traceback" all the way to the last line containing the exception message. Commented Dec 3, 2012 at 21:35

2 Answers 2

4

MIMEText takes a string as its first argument. You're creating text1 as a tuple. You need something more like

"First Name: %s\nLast Name: %s" % (first_name, last_name)  
Sign up to request clarification or add additional context in comments.

2 Comments

It's worth noting that the OP's original code was text1 = "First Name: ", first_name, "\nLast Name: ", last_name. This is an understandable conflation with the functionality of the print statement that allows it to take multiple arguments and join them together with spaces. This is not, however, how one normally concatenates strings in Python.
Thank you, I can figure out the rest from here :). Much appreciated.
1

you need to attach part1 to msg:

msg.attach(part1)

you can also find a good example of how to send a multipart email message in the Python Documenation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.