An easy-to-use interface for PHPMailer that takes a JSON object and emails the values, replying with JSON
config.php
valuessend-email.php
This is project is very opinionated. If the majority think a change would be useful, I'll happily comply. Otherwise, feel free to open a PR or submit ideas.
'encryption_type'
is not set, neither will SMTPSecure
bevalue
has to be set to something, even if just an empty string. Otherwise value
will default to null
, a fail conditionincludes/create-default-email-body.php
If the beginning of the JSON submitted key(separated by a dash) matches one of these strings, the format used to validate/clean the value will automatically set to the corresponding format shown here if no 'format' was passed in the JSON array.
Example: The values under keys "phone-cell"
, "phone-home"
, and "phone-intergalactic"
will be validated/parsed as phone numbers, unless their "format" is set explicitly.
// Default keys on the server
$default_key_formats = [
'name' => 'text', // or string
'phone' => 'phone',
'email' => 'email',
'link' => 'url',
'url' => 'url',
'float' => 'float',
'int' => 'int',
'num' => 'int'
];
If no format is sent, it defaults to 'text'. You can override default formats(see above) by sending them in the array
{ // Pretend this is the JSON sent
"name": "Man Guy",
// -- OR ---
"name": {
"format" : "text",
"value" : "Man Guy"
},
"email" : "[email protected]"
// -- OR ---
"email" : {
"format" : "email",
"value" : "[email protected]"
},
// -- ETC ---
"phone" : {
"format" : "phone",
"value" : "(555) 555-5555"
}
}
All replies are in JSON, and comply(mostly) with JSend response shape.
A "status"
of "fail"(with data) or "success" always returns the received data unchanged under the "raw"
key,
while a "fail" status sets the reason a key failed under "failed"
{ // Server errors
"status" : "error",
"message" : "Error message"
}
{ // User errors
"status" : "fail",
"data" : {
// Holds keys of any values that failed and their failure message
"failed": {
"phone": "Invalid format",
"email": "Invalid format",
"message": "No value recieved",
"favorite-food" : "Required; no key sent",
"hidden-field" : "Value did not match required value",
/*,...etc*/
},
// Values the server received(unchanged)
"raw": {
"name" : "(Raw value)",
"phone" : "(Raw value)",
"email" : "(Raw value)"
// ...etc
},
// The values after being sanitized
"sanitized": {
"name" : "(Sanitized value)",
"phone" : "(Sanitized value)",
"email" : "(Sanitized value)"
}
}
}
-- OR --
{ // User errors
"status" : "fail",
"message" : "Error message"
}
{ // Success
"status" : "success",
"data" : {
// Values the server received
"raw": {
"name" : "(Raw value)",
"phone" : "(Raw value)",
"message" : "(Raw value)"
// ...etc
},
// The values after being sanitized
"sanitized": {
"name" : "(Sanitized value)",
"phone" : "(Sanitized value)",
"email" : "(Sanitized value)"
}
}
// Values will be sanitized as per their format before being emailed.
}
// All addresses can be formatted as an array with shape
// ['[email protected]', 'A name to send by']
// GLOBAL['ezee_email_vals'][('post-key')] holds all sanitized input values if everything goes well
global $ezee_email_vals;
$name = $ezee_email_vals['name'];
$email = $ezee_email_vals['email'];
$message = $ezee_email_vals['message'];
// SMTP is always true
// Configuration for where the email will be sent from (required)
$ezee_email_send_from_config = [
'encryption_type' => 'tls', // (optional) 'ssl' or 'tls'
'port' => 587,
'server' => 'smtp.gmail.com', // Can also take secondary server separated by a comma
'email' => '[email protected]', // Email address on server
// or ['address', 'name'](name will be applied to send_as)
'password' => '' // Password for address server
'send_as' => '[email protected]' // (optional) defaults to value in 'email'
// or ['address', 'name']
];
// Config for where the email will be sent to (required)
$ezee_email_send_to_config = [
// NOTE: If using a name for the first address, this must
// be an array of arrays.
// Otherwise it will try to send the 'name' an email, resulting in failure.
'addresses' => 'addresses' => [
'[email protected]',
['[email protected]', 'Man Guy'],
[ '[email protected]', 'Mr Stark', true ] // <-- Will be CC
],
// [email protected]
// ['[email protected]', 'Maximus Prime']
/* Multiple addresses can be specified, defaults to BCC for each address after the first(see notes)
[
// Setting the third array value (or array[2]) to 'true' will make the email CC
['[email protected]', 'Maximus Prime', true],
['[email protected]', 'Maximus Prime']
]
*/
'subject' => "Contact from $name", // Sanitized values can be used here(see above)
'reply_to' => '[email protected]' // (optional)
// ['[email protected]', 'my name']
];
// Flags and required values for POSTed JSON (required)
$ezee_email_value_options = [
// Only email values under 'required_vals' keys (default true)
'limit_to_required' => true,
// Request fails if 'limit_to_required' is true (default true)
// and more inputs than required are posted
'fail_on_value_overload' => false,
// Required posted keys and values
// If required value is set to null, the received value can be anything(*anything but null).
'required_values' => [
// e.g., the 'name' key could hold a number, or text
'name' => null,
/* This is for if you have an optional input, like a
message, that may or may not be submitted at all */
'optional-val' => '(opt)',
// For clarity: this would be like a text box with
// the 'name' set to 'two-plus-two' and value set to '4'
'two-plus-two' => '4',
]
];
// is_html defaults to true, and uses msgHTML which automatically builds a plain text version if needed.
// Everything below is optional, even the $ezee_email_body_config variable
$ezee_email_body_config = [
'word_wrap'=> 50, // Defaults to 72, set 0 for no wrapping
'is_html' => true, // Defaults to true
'template' => "
<html>
<h2>New contact request</h2>
<div>
My name is $name. I used to be a spy, until...
</div>
<br />
<div>
<b>Hey look, an email!</b> $email
</div>
<br />
<div>
<b>This is a Message</b>
<p>$message </p>
</div>
</html>
"
];
global $ezee_email_vals;
$name = $ezee_email_vals['name'];
// Where email is sent from
$ezee_email_send_from_config = [
'encryption_type' => 'tls', // 'ssl' or 'tls'
'port' => 587,
'server' => 'smtp.gmail.com',
'email' => '[email protected]',
'password' => 'xxxxxxxx' // Password for address server
];
// Where email is sent to
$ezee_email_send_to_config = [
// You can send the email to yourself
'addresses' => '[email protected]',
'subject' => "Contact from $name",
];
// With both flags off, you don't have to set required_values
$ezee_email_value_options = [
'limit_to_required' => false,
'fail_on_value_overload' => false,
];
// Uses default email body