Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3622842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:18:34+00:00 2026-05-18T23:18:34+00:00

I have a app with a kind of rest api that I’m using to

  • 0

I have a app with a kind of rest api that I’m using to send emails . However it currently sends only text email so I need to know how to modify it and make it send html . Below is the code :

from __future__ import with_statement

#!/usr/bin/env python
#

import cgi
import os
import logging
import contextlib
from xml.dom import minidom
from xml.dom.minidom import Document
import exceptions
import warnings
import imghdr
from google.appengine.api import images
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import mail
import wsgiref.handlers

# START Constants
CONTENT_TYPE_HEADER = "Content-Type"
CONTENT_TYPE_TEXT = "text/plain"
XML_CONTENT_TYPE = "application/xml"
XML_ENCODING = "utf-8"
"""
Allows you to specify IP addresses and associated "api_key"s to prevent others from using your app.
Storage and Manipulation methods will check for this "api_key" in the POST/GET params.
Retrieval methods don't use it (however you could enable them to use it, but maybe rewrite so you have a "read" key and a "write" key to prevent others from manipulating your data).

Set "AUTH = False" to disable (allowing anyone use your app and CRUD your data).

To generate a hash/api_key visit https://www.grc.com/passwords.htm
To find your ip visit http://www.whatsmyip.org/
"""
AUTH = {
    '000.000.000.000':'JLQ7P5SnTPq7AJvLnUysJmXSeXTrhgaJ',
}
# END Constants

# START Exception Handling
class Error(StandardError):
    pass
class Forbidden(Error):
    pass

logging.getLogger().setLevel(logging.DEBUG)

@contextlib.contextmanager
def mailExcpHandler(ctx):
    try:
        yield {}
    except (ValueError), exc:
        xml_error_response(ctx, 400 ,'app.invalid_parameters', 'The indicated parameters are not valid: ' + exc.message)
    except (Forbidden), exc:
        xml_error_response(ctx, 403 ,'app.forbidden', 'You don\'t have permission to perform this action: ' + exc.message)
    except (Exception), exc:
        xml_error_response(ctx, 500 ,'system.other', 'An unexpected error in the web service has happened: ' + exc.message)

def xml_error_response(ctx, status, error_id, error_msg):
    ctx.error(status)
    doc = Document()
    errorcard = doc.createElement("error")
    errorcard.setAttribute("id", error_id)
    doc.appendChild(errorcard)
    ptext = doc.createTextNode(error_msg)
    errorcard.appendChild(ptext)
    ctx.response.headers[CONTENT_TYPE_HEADER] = XML_CONTENT_TYPE
    ctx.response.out.write(doc.toxml(XML_ENCODING))
# END Exception Handling

# START Helper Methods
def isAuth(ip = None, key = None):
    if AUTH == False:
        return True
    elif AUTH.has_key(ip) and key == AUTH[ip]:
        return True
    else:
        return False

# END Helper Methods


# START Request Handlers
class Send(webapp.RequestHandler):
    def post(self):
        """
        Sends an email based on POST params. It will queue if resources are unavailable at the time.

        Returns "Success"

        POST Args:
            to: the receipent address
            from: the sender address (must be a registered GAE email)
            subject: email subject
            body: email body content
        """
        with mailExcpHandler(self):
            # check authorised
            if isAuth(self.request.remote_addr,self.request.POST.get('api_key')) == False:
                raise Forbidden("Invalid Credentials")

            # read data from request
            mail_to = str(self.request.POST.get('to'))
            mail_from = str(self.request.POST.get('from'))
            mail_subject = str(self.request.POST.get('subject'))
            mail_body = str(self.request.POST.get('body'))

            mail.send_mail(mail_from, mail_to, mail_subject, mail_body)

            self.response.headers[CONTENT_TYPE_HEADER] = CONTENT_TYPE_TEXT
            self.response.out.write("Success")


# END Request Handlers

# START Application
application = webapp.WSGIApplication([
                                                                             ('/send', Send)
                                                                         ],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

# END Application
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:18:34+00:00Added an answer on May 18, 2026 at 11:18 pm

    Have a look to the Email message fields of the send_mail function.
    Here is the parameter you need:

    html
    An HTML version of the body content, for recipients that prefer HTML email.

    You should add the html input parameter like this:

    #Your html body
    mail_html_body = '<h1>Hello!</h1>'
    
    # read data from request
    mail_to = str(self.request.POST.get('to'))
    mail_from = str(self.request.POST.get('from'))
    mail_subject = str(self.request.POST.get('subject'))
    mail_body = str(self.request.POST.get('body'))
    
    mail.send_mail(mail_from, 
                   mail_to,
                   mail_subject, 
                   mail_body,
                   html = mail_html_body ) #your html body
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app using PHP and the PayPal API. The basic way it
I have an app that subscribes to a specific kind of notifications from the
I have a client app that communicates to the spring server with REST type
I have a Grails app running on 1.3.7 with the json-rest-api plugin version 1.0.8
Let's say I have an app that matches two People against each other, kind
I have an Expando model kind in my App Engine datastore and I'm setting
have an app that finds your GPS location successfully, but I need to be
Have an app that has listings - think classified ads - and each listing
I have an app that needs to open a new window (in the same
I have an app written in C# that lies on a network share. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.