Hey so I have a mail script written in python which works fine when I run 
it from a linux terminal. My issue is I need to make an ajax call passing 
in parameters from a custom form I created. In the Javascript it calls the 
script but the mail never sends. I am stumped I have been trying to get the 
ajax to work for days now with no luck. I am currently running the web2py 
application on localhost:8000. Below I will post the code in question.

*HTML Code*
       <div class="title"><p>Contact Us</p></div>
       <div class="form-piece">
           <form enctype="multipart/form-data">
               <input required class="in-txt" id="name" name="name" 
placeholder="Your Name" type="text" value=""/><br>
               <input required class="in-txt" id="email" name="email" 
placeholder="Your Email" type="email" value=""/><br>
               <textarea required class="last in-area" id="message" 
cols="40" name="message" placeholder="Write your message here." rows="10" 
type="text"></textarea><br>
               <div class="error-message-container"><p 
class="error-message" id="contact-message"></p></div>
               <input class="button mail" id="send-contact" type="button" 
value=""/>
           </form>
       </div><!--.form-piece-->

*JS Code (This is fired via a call to onclick of the (#send-contact) button 
above)*
   function sendContactEmail()
    {
        var name = $('#name').val();
        var email = $('#email').val();
        var message = $('#message').val();
//Variables are not currently used but will be passed once the cgi works 
properly...

$.ajax({
        type: "POST",
url: "../static/python/the_mailer.py"
});
    }

*Python Code (This works if using the linux terminal (Comman: python 
the_mailer.py) but not via ajax)*
import smtplib, socket, sys, getpass

def main():
print

try:
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
print 'Connection to Gmail Succesfully'
print 'Connected to Gmail \n' 
try:
gmail_user = "[email protected]"
gmail_pwd = "my pasword"
smtpserver.login(gmail_user,gmail_pwd)
except smtplib.SMTPException:
print 'Authentication failed \n'
smtpserver.close()
getpass.getpass('Press ENTER to continue...')
sys.exit(1)

except (socket.gaierror, socket.error, socket.herror, 
smtplib.SMTPException), e:
print 
print 'Connection to Gmail failed'
print e
getpass.getpass('Press ENTER to continue...')
sys.exit(1)
while True:
to = "[email protected]"
if to != '': break
else: print 'this field is required'

sub = "Testing The Email Feature"
bodymsg = "This is an automated test from Application"
print 
header = 'To:'+to+'\n'+'From:'+gmail_user+'\n'+'Subject:'+sub+'\n'
print header
msg = header+'\n'+bodymsg+'\n\n' 

try:
smtpserver.sendmail(gmail_user,to,msg)
except smtplib.SMTPException:
print 'Email could not be sent \n'
smtpserver.close()
getpass.getpass('Press ENTER to continue...')
sys.exit(1)

print 'Email Sent Sucessfully! \n'
smtpserver.close()
sys.exit(1)

print '\n'
# raw_input('pause program')

main()


-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to