Send email using PHP / AJAX from an HTML form

I want to send information that a user filled out from an HTML form to my email address. In my opinion, this cannot be done using only client-side encoding due to the nature of the email and suggesting that you use PHP (in combination with AJAX) to process server-side code. I followed the guide here , but I do not receive any emails to my email address. I test locally on my machine (using XAMPP) before deploying my code to my client web space (goDaddy). I want to note that I have never used PHP before.

JavaScript:

var data = "This is my email"; $.ajax({ type: "POST", url: "email.php", data: data, dataType: "text" }); 

PHP (email.php):

 <?php $to = " myself@hotmail.com "; $subject = "This is my email"; $message = $_REQUEST; $send = mail($to, $subject, $message); if(!$send){ die(); } ?> 
+4
source share
5 answers

You must replace $_REQUEST with $_REQUEST["data"] or something like this, because $_REQUEST is an array.

+2
source

all page refresh time. Below is my code:

HTML 5:

 <form method="post" data-ajax="false"> <input type="text" name="email" placeholder=" jack@example.com , jil@example.com "> <input type="submit" name="submit" value="Invite" class="submitBtn"> </form> 

JS:

 $('form').bind('submit',function(){ $.ajax({ type : 'POST', url : 'data/invite.php', data : $(this).serialize(), cache : false, dataType: 'text', success : function (serverResponse) { alert('mail sent successfully.'); }, error : function (jqXHR, textStatus, errorThrown) {alert('error sending mail');} }); }) 

PHP:

 <?php $to = $_POST['mail']; $name = 'Indusnet Technologies'; $subject = 'Think Recycle'; $text = 'Welcome to our site'; $message =' You received a mail from '.$name; $message .='Text of the message : '.$text; if(mail($to, $subject,$message)){ echo 'mail successful send'; } else{ echo 'there's some errors to send the mail, verify your server options'; } ?> 
+1
source
 var data = "This is my email"; $.ajax({ type: "POST", url: "email.php", data: data, dataType: "text" }); 

This code will look as follows

 var data = "This is my email"; $.ajax({ type: "POST", url: "email.php", data: {"data": data}, dataType: "text" }); 

and get the code either through $ _POST ['data'] or $ _REQUEST ['data']

0
source

You should probably return false in the form. Try the following:

 $('form').submit(function(){ $.ajax({ type : 'POST', url : 'data/invite.php', data : $(this).serialize(), cache : false, dataType: 'text', success : function (serverResponse) { alert('mail sent successfully.'); }, error : function (jqXHR, textStatus, errorThrown) {alert('error sending mail');} }); return false; }) 
0
source

All Articles