Une interface facile à utiliser pour PHPMailer qui prend un objet JSON et envoie les valeurs par courrier électronique, en répondant avec JSON
config.php
send-email.php
Ce projet est très opiniâtre. Si la majorité pense qu’un changement serait utile, je m’y conformerai avec plaisir. Sinon, n'hésitez pas à ouvrir un PR ou à soumettre des idées.
'encryption_type'
n'est pas défini, SMTPSecure
ne sera pas non plusvalue
doit être définie sur quelque chose, même s'il s'agit simplement d'une chaîne vide. Sinon, value
par défaut sera null
, une condition d'échecincludes/create-default-email-body.php
Si le début de la clé soumise JSON (séparée par un tiret) correspond à l'une de ces chaînes, le format utilisé pour valider/nettoyer la valeur sera automatiquement défini sur le format correspondant affiché ici si aucun « format » n'a été transmis dans le tableau JSON.
Exemple : Les valeurs sous les clés "phone-cell"
, "phone-home"
et "phone-intergalactic"
seront validées/analysées en tant que numéros de téléphone, à moins que leur "format" ne soit défini explicitement.
// 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 '
];
Si aucun format n'est envoyé, la valeur par défaut est « texte ». Vous pouvez remplacer les formats par défaut (voir ci-dessus) en les envoyant dans le tableau
{ // 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"
}
}
Toutes les réponses sont en JSON et sont conformes (pour la plupart) à la forme de réponse JSend. Un "status"
"échec" (avec données) ou "succès" renvoie toujours les données reçues inchangées sous la clé "raw"
, tandis qu'un statut "échec" définit la raison pour laquelle une clé a échoué sous "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)"
}
}
}
-- OU --
{ // 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