Send simple email with Twig template through POST HTTP requests. This is a PHP service that takes the basic elements required to send an email with mail()
PHP function or SMTP with PHPMailer. The key / values are taken from data parameter and replaced through the template selected by the template
or remote_template
variable.
- Email
.html
templating with{{...}}
Twig moustaches - Send email with a POST HTTP request
Download and unzip the repository content to your host or service. E.g. : example.com/services/send-email
.
HTTP Post parameters :
Name | Type | Description | Example |
---|---|---|---|
to | string | Email receiver. Required. | to@example.com |
from | string | Email sender. Required. | from@example.com |
from_name | string | Email sender name | John |
subject | string | Email subject. Required. | Example subject |
template | string | Template name without html extension and language. You can add your templates in the /templates directory. |
example |
lang | string | Template language. Append a -lang to the html template. |
en |
remote_template | string | You can fetch a html remote file. | https://example.com/template.html |
message | string | You can write your own message if you don't want to use a template | Hi, this is my message |
data | Object (JSON) | The key/values are taken from data and replaced through the template. | {"name": "John Doe", "message": "Un petit message"} |
transport | string | Transport type. If you have not SMTP server, don't use this parameter. | smtp or mail |
curl --request POST \
--url https://example.com/services/send-email/ \
--header 'content-type: multipart/form-data;' \
--form 'to=<EMAIL_TO>' \
--form 'from=<EMAIL_FROM>' \
--form 'template=<FILE_OR_URL>' \
--form data=<JSON_DATA> \
--form subject=<SUBJECT>
var data = new FormData();
data.append("to", "<EMAIL_TO>");
data.append("from", "<EMAIL_FROM>");
data.append("template", "<FILE_OR_URL>");
data.append("data", JSON.stringify(<JSON_DATA>));
data.append("subject", "<SUBJECT>");
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://example.com/services/send-email/");
xhr.send(data);
var form = new FormData();
form.append("to", "<EMAIL_TO>");
form.append("from", "<EMAIL_FROM>");
form.append("template", "<FILE_OR_URL>");
form.append("data", JSON.stringify(<JSON_DATA>));
form.append("subject", "<SUBJECT>");
fetch("https://example.com/services/send-email/", {
"method": "POST",
"headers": {
"content-type": "multipart/form-data;"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
var request = require("request");
var options = {
method: 'POST',
url: 'https://example.com/services/send-email/',
headers: {'content-type': 'multipart/form-data;'},
formData: {
to: '<EMAIL_TO>',
from: '<EMAIL_FROM>',
template: '<FILE_OR_URL>',
data: '<JSON_DATA>',
subject: '<SUBJECT>'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
<form class="" action="https://example.com/services/send-email/" method="post">
<input type="hidden" name="to" value="<EMAIL_TO>">
<input type="hidden" name="from" value="<EMAIL_FROM>">
<input type="hidden" name="subject" value="<SUBJECT>">
<input type="hidden" name="template" value="<FILE_OR_URL>">
<!--<input type="hidden" name="message" value="<MESSAGE>">-->
<input type="hidden" name="data" value=""> <!-- to JSON -->
<input type="submit" value="Send email">
</form>
<input type="button" value="Send Email" onclick="sendEmail()">
<script>
function sendEmail() {
var templateData = {
data_one: 'One',
data_two: 'Two',
};
var data = new FormData();
data.append('to', '<TO>');
data.append('from', '<FROM>');
data.append('subject', '<SUBJECT>');
data.append('message', '<MESSAGE>');
data.append('data', JSON.stringify(templateData));
var request = new XMLHttpRequest();
request.open("POST", 'https://example.com/services/send-email/');
request.send(data);
}
</script>
We’re really happy to accept contributions from the community, that’s the main reason why we open-sourced it! There are many ways to contribute, even if you’re not a technical person.
- Fork it (https://github.com/lumibib/send-email/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
- …
- Michael Ravedoni - Initial work - michaelravedoni
See also the list of contributors who participated in this project.