Amyth
Amyth

Reputation: 32949

Python : smtplib sendmail function

i have an activation mail sending script as follows.

#!/usr/bin/python

__author__ = 'Amyth Arora (***@gmail.com)'

import smtplib
import string
import sys
import random

from email.MIMEText import MIMEText


def generate_activation_key(size=64, chars=string.ascii_lowercase + string.digits):
  return ''.join(random.choice(chars) for x in range(size))


def generate_activation_url(key):
  return 'http://www.example.com/users/activate/' + key

def Activate(name, to_addr):
  sender, reciever = '[email protected]', to_addr
  act_url = generate_activation_url(generate_activation_key())
  main_mssg = """
  Dear %s,

  Thakyou for creating an account with Example.com. You are now just one click away from using your example account. Please click the following link to verify this email address and activate your account. 

  Please Note, You must complete this step to become a registered member of example. you will only need to visit this url once in order to activate your account.

  ============================
  Activation Link Below:
  ============================

  %s


  if you are facing problems activating your account please contact our support team at
  [email protected]

  Best Regards,

  Example Team
  """ % (name, act_url)


  message = MIMEText(main_mssg)
  message['From'] = 'Example <' + sender + '>'
  message['To'] = reciever
  message['MIME-Version'] = '1.0'
  message['Content-type'] = 'text/HTML'
  message['Subject'] = 'Activate Your Example Account'
  try:
    smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
    smtpObj.set_debuglevel(1)  # Swith On/Off Debug Mode
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.ehlo()
    smtpObj.login('[email protected]', 'mypass')
    smtpObj.sendmail(sender, reciever, message.as_string())
    smtpObj.close()
    return act_url
  except:
    return None

def main(argv):
  if len(argv) != 2:
    sys.exit(1)
  Activate(argv[0], argv[1])


if __name__ == '__main__':
  main(sys.argv[1:])

The script works fine as i have tried it through command line like this :

./scriptname 'Reciepent Name' '[email protected]'

however i want to call the activation script from within one of my application handlers like this.

import activation

act_key = activation.Activate('Reciepent Name', '[email protected]')

but when i do this the script returns None. Can anyone figure out how to fix this ?

Upvotes: 2

Views: 2740

Answers (2)

Mark Finch
Mark Finch

Reputation: 766

If all you need is to send Activation Emails my AE-BaseApp project has a working example you can use. AE-BaseApp/Python has a link to my Github repository where you can fork or download the code. I also have a working handler to receive/reply to mail.

Upvotes: 1

Sebastian Kreft
Sebastian Kreft

Reputation: 8189

App Engine does not allow the users to send emails using the smtplib library. Instead you need to use the api provided. The documentation can be found here: https://developers.google.com/appengine/docs/python/mail

Upvotes: 7

Related Questions