Implements a simple form handler for plain html forms. Great for static website. Sets up Lambda and API Gateway.
This modules creates and API Gateway POST resource and connects this to a lambda function. When a HTML form is submitted the API Gateway forwards the formdata to the lambda function and this sends the email.
You need a configured SES domain or SES emails available in the same account.
module "form_action_example_com" {
source = "TechNative-B-V/html-form-action/aws"
name = "example-com-form-action-handler"
to_email = "[email protected]" # Make sure SES accepts this email address or complete domain
from_email = "[email protected]" # Make sure SES accepts this email address or complete domain
allowed_origin = "*" # You should set this to the website url when live
}
output "form_action_example_com_url_for_form" {
description = "Use this URL in your the action attribute of your form element."
value = module.form_action_example_com.message_post_url
}
The form html looks like this.
<form action="https://XXXXXXXXXX.execute-api.eu-central-1.amazonaws.com/main/message" method="post">
<!-- FORM CONFIGURATION -->
<input type="hidden" name="_subject" value="Demo Form Submission">
<input type="hidden" name="_success_url" value="http://example.com/form_success.html">
<input type="hidden" name="_fail_url" value="http://example.com/form.html">
<!-- FORM FIELDS -->
<input placeholder="Full Name" type="text" name="full-name"><br>
<input placeholder="Email" type="text" name="Email"><br>
<textarea name="message" placeholder="Your message"></textarea><br>
<input type="submit" value="send"></br>
</form>
The form html looks like this.
<form action="https://XXXXXXXXXX.execute-api.eu-central-1.amazonaws.com/main/message" method="post">
<!-- FORM CONFIGURATION -->
<input type="hidden" name="_subject" value="Demo Form Submission">
<input type="hidden" name="_success_url" value="http://example.com/form_success.html">
<input type="hidden" name="_fail_url" value="http://example.com/form.html">
<!-- Set the field in this form which contains the visiter email address to send a reply mail to -->
<input type="hidden" name="_visiter_email_field" value="Email">
<!-- This field contains a link to a mail template. This is html file which is used to create a reply -->
<input type="hidden" name="_reply_mail_template" value="http://example.com/mail_template.html">
<!-- FORM FIELDS -->
<input placeholder="Full Name" type="text" name="full_name"><br>
<input placeholder="Email" type="text" name="Email"><br>
<textarea name="message" placeholder="Your message"></textarea><br>
<input type="submit" value="send"></br>
</form>
The mail_template could look like this. The title is used as subject. The form handler will try to substitute variables with the fields from the form.
<html>
<head>
<title>Mailform submitted</title>
</head>
<body>
Dear $full_name,
Your mail has been sent.
Cheers!
</body>
</html>
AWS_PROFILE=some-profile python lambda_src/html_form_action.py
Name | Version |
---|---|
aws | >= 4.0.0 |
Name | Source | Version |
---|---|---|
lambda_function | terraform-aws-modules/lambda/aws | 3.3.1 |
resource_cors | mewa/apigateway-cors/aws | 2.0.0 |
Name | Type |
---|---|
aws_api_gateway_deployment.main | resource |
aws_api_gateway_integration.message | resource |
aws_api_gateway_method.message | resource |
aws_api_gateway_resource.message | resource |
aws_api_gateway_rest_api.main | resource |
aws_lambda_permission.lambda_permission | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
allowed_origin | Which origin to allow submissions from. Use * when testing | string |
"*" |
no |
from_email | Receiving email address for forwarded messages, can also be configured in html form | string |
"" |
no |
name | Name to use for function and api gateway | string |
n/a | yes |
to_email | 'From' email to use when forwarding a message, defaults to recipient email in the Lambda, can also be configured in html form | string |
"" |
no |
Name | Description |
---|---|
message_post_url | POST URL for message requests |