Отправить электронное письмо очень просто, вам не нужен плагин. Сначала давайте подготовим вашу форму;добавьте эти атрибуты в форму.
<form id="contact-form" method="POST" action="PHP/mailto.php">
теперь пришло время настроить скрипт js:
// first things first you gotta listen to the submit event and stop it from
// performing the 'action' which is redirecting the form data to the php
// file we are going to create.
document.getElementById('contact-form').addEventListener('submit', function(event){
//this prevents the form from doing its default action: redirection
event.preventDefault();
// disable inputs so form can only be sent once
$disabled_inputs = document.querySelectorAll('input, textarea');
for(i = 0; i < $disabled_inputs.length; i++){
$disabled_inputs[i].disabled = true;
}
// empty JSON object that will hold form data
var data = {};
//this function will loop through the data and collect the info on the inputs
// and text areas with a name attribute
this.querySelectorAll('input[name], textarea[name]').forEach(function($input) {
// this formats the data in a JSON object {inputname : inputvalue}
$data[$input.getAttribute('name')] = $input.value;
});
// take a look at the console so you get used to the json format
console.log($data);
// this will store the data in a JSON object so you have to clean it to send
// it to the php file with a post variable set
var json_upload = "your_php_postvariable=" + JSON.stringify($data);
// open new ajax request
var inquriry_request = new XMLHttpRequest();
// declare ajax method and origin (look at the form element)
inquriry_request.open("POST", "/PHP/mailto.php");
// declare cleaned JSON format to PHP file
inquriry_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// send ajax request with data
inquriry_request.send(json_upload);
// this functions runs after ajax request loads response
inquriry_request.onload = function(){
// create new script element
scripter = document.createElement('script');
// declare javascript type
scripter.type = 'text/javascript';
// load script echoed from php file inside this tag
scripter.innerHTML = inquriry_request.responseText;
// append script to body (this will cause script to run)
document.querySelector('body').append(scripter);
}
});
теперь позволяет настроить файл mailto.php:
<?php
// run this function if post variable is set
if(isset($_POST['your_php_variable']){
// this while output an error log inside the /PHP/ directory
error_log(0);
// html email headers
$headers = "From: Web Emails <your@email.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// php post variable loop input attributes
var $php_post_var_loop = $_POST['your_php_variable'];
$name = $php_post_var_loop['user_name'];
$email = $php_post_var_loop['user_email'];
$message = $php_post_var_loop['message'];
// strings in php are appended by dots
$email_text = "From: ". $name . "<br><br>".
"Email: " . $email . "<br><br>".
"Message: " . $message;
// run if statement to check if php function ran to send mail
if(@mail('your@email.com', 'Email Subject', $email_text, $headers)){
// use this if you have to switch between single and double quotes
// inside your js script
$quo = '"';
echo "//js code to run if mail sends!"
} else {
// use this if you have to switch between single and double quotes
// inside your js script
$quo = '"';
echo = "//js code to run if mail does not send"
}
}
?>