Do I use GET or POST sending mail?

8 views
Skip to first unread message

Zeynel

unread,
Nov 18, 2010, 9:25:54 PM11/18/10
to Google App Engine
The tutorial here http://code.google.com/appengine/docs/python/mail/sendingmail.html
has POST:

class InviteFriendHandler(webapp.RequestHandler):
@login_required
def post(self):
to_addr = self.request.get("friend_email")
....

But when I tried the same code with POST it did not send any mail.
Then I changed it to GET and it sent the email. Can you explain the
logic behind using GET or POST when sending email. This is the code I
am using:

class InviteFriendHandler(webapp.RequestHandler):
#@login_required
# change "def post(self):" of the tutorial to "def get(self):"
def get(self):
confirmation_url = "http://example.com/"
to_addr = self.request.get("friend_email")

message = mail.EmailMessage()
message.sender = users.get_current_user().email()
message.to = to_addr
# tutorial is missing subject which throws error.
message.subject = "hello"
message.body = "Click on the link %s " % confirmation_url

message.send()

The form from which I get "friend_email" is in the MainPage:

class MainPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
greeting = None
if user:
greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
(user.nickname(),
users.create_logout_url("/")))
else:
greeting = ("<a href=\"%s\">Sign in or register</a>" %
users.create_login_url("/"))

self.response.out.write("""
<html>
<body>
<form action="/invite" method="get">
email: <input type="text" name="friend_email" />
<input type="submit" value="Submit" />
</form>
</body>
<html>""")

self.response.out.write(greeting)

Gaurav Vaish

unread,
Nov 18, 2010, 10:09:58 PM11/18/10
to Google App Engine
Post. And I'd recommend doing a redirect (302) after the mail is sent
so that if the user accidently presses the refresh button, mail is not
resent.

--
Happy Hacking,
Gaurav Vaish
www.mastergaurav.com


On Nov 19, 7:25 am, Zeynel <azeyn...@gmail.com> wrote:
> The tutorial herehttp://code.google.com/appengine/docs/python/mail/sendingmail.html

Zeynel

unread,
Nov 18, 2010, 10:15:52 PM11/18/10
to Google App Engine
On Nov 18, 10:09 pm, Gaurav Vaish <gaurav.va...@gmail.com> wrote:
> Post.

Thanks Gaurav. Can you tell why POST is not working in the code that I
am using? And how to fix it so that I can use POST.

Robert Kluin

unread,
Nov 18, 2010, 10:47:24 PM11/18/10
to google-a...@googlegroups.com
In your form you specify the method as get. Change it to post.

<form action="/invite" method="post">


Robert

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Zeynel

unread,
Nov 18, 2010, 11:15:57 PM11/18/10
to Google App Engine
On Nov 18, 10:47 pm, Robert Kluin <robert.kl...@gmail.com> wrote:
> In your form you specify the method as get.  Change it to post.
>
>     <form action="/invite" method="post">

Great! Thanks. I had to change the handler too:

def post(self):

Reply all
Reply to author
Forward
0 new messages