How to get Form values in Python code and Send Email

Mike Kazantsev mk.fraggod at gmail.com
Wed May 20 23:04:11 EDT 2009


On Wed, 20 May 2009 17:49:47 +0530
Kalyan Chakravarthy <kalyanchakravarthy at hyit.com> wrote:

> Hi
>     Now i can able to get the form details in to python code,
> 
> can any one tell me the format to send form values to one Emil
> id   ...  for this  I required SMTP set up?

You can use email and smtplib modules for that along with any SMTP
relay you have access to, even if it requires authentication - smtplib
has support for that.

Following example doesn't uses authentication for SMTP so you might want
to add it or you can indeed set up some SMTP on local machine (there
are really simple ones like ssmtp or msmtp, that can relay mail to
auth-enabled host).


  import smtplib
  from email.MIMEMultipart import MIMEMultipart
  from email.MIMEBase import MIMEBase
  from email.MIMEText import MIMEText
  from email.Utils import COMMASPACE, formatdate
  from email import Encoders
  import os, collections

  def send(to, subj, body, files=[], from=None, relay='localhost'):
    if not isinstance(to, collections.Iterable): to = (to,)

    msg = MIMEMultipart()
    msg['From'] = from
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subj

    msg.attach( MIMEText(body) )

    for file in files:
      part = MIMEBase('application', "octet-stream")
      part.set_payload( open(file,"rb").read() )
      Encoders.encode_base64(part)
      part.add_header('Content-Disposition', 'attachment;
    filename="%s"'% os.path.basename(file)) msg.attach(part)

    smtp = smtplib.SMTP(relay)
    smtp.sendmail(from, to, msg.as_string() )
    smtp.close()



-- 
Mike Kazantsev // fraggod.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090521/21ce1c61/attachment-0001.sig>


More information about the Python-list mailing list