0
from smtplib import SMTP_SSL as SMTP
from email.MIMEText import MIMEText
import traceback

#from_field is a dictionary with smtp_host, name, email, and password
def send(from_field, to_email):    
    subject = 'how do you know it'
    body="""\
    hello there
    """

    try:
        msg = MIMEText(body, 'plain')
        msg['Subject']= subject
        msg['From']   =  from_field['name'] + ' <'+from_field['email']+'>'

        print msg

        conn = SMTP(from_field['smtp'])
        conn.set_debuglevel(False)
        conn.login(from_field['email'],from_field['password'])
        try:
            conn.sendmail(from_field.email,[to_email], msg.as_string())
            pass
        finally:
            conn.close()

    except Exception, exc:
        traceback.print_exc()
        sys.exit( "mail failed; %s" % str(exc) ) # give a error message    

I get this error message when I run it:

AttributeError: 'dict' object has no attribute 'email' on the conn.sendmail line

1 Answer 1

2

from_field is a dictionary. That line should be:

conn.sendmail(from_field['email'], to_email, msg.as_string())
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Should "to_email" be a list?
@TIMEX It's hard to say from the code you posted. It depends on what type to_email is when it's passed in to the send function.

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.