I had this similar case in one of the project in which the user's (site visitor) query was to be sent to site admin (gmail account). Have done this with Ajax call sending the details collected on the form, posted to php file that uses Postfix to send mail. (The website was hosted on Ubuntu Server).
The JQuery code in the page is given below.
$("#btn_email").click(function () {
//get input field values data to be sent to server
post_data = {
'vemail': $('input[name=email]').val(),
'query' : $('input[name=query]').val(),
'subject': 'New Query',
'msg': 'There is new request for query'
};
//Ajax post data to server
$.post('sendmail.php', post_data, function (response) {
if (response.type == 'error') { //load json data from server and output message
output = '<div class="error">' + response.text + '</div>';
} else {
output = '<div class="success">' + response.text + '</div>'; }}});
The PHP code that handles the request as
$email=$_POST['email'];
$subject = 'New Query';
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$message = $_POST['msg']. "\r\n" .$_POST['query'] . "\r\n\r\nEmail : " . $email ;
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
$send_mail = mail("mycontact@gmail.com", $subject, $message, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi, We have added '.$user_email .' to the query. Will answer you soon. Thank you for your interest.'));
die($output);
}
}