从HTML表单使用PHP / AJAX发送电子邮件

时间:2022-10-16 10:58:29

I want to send the information the user has filled out from an HTML form to my email address. From my understanding, this cannot be done by using only client-side coding due to the nature of how emails work, and suggested to use PHP (combined with AJAX) to handle the server-side code. I followed the guide here but I am not receiving any emails at my email address. I am testing locally on my machine (using XAMPP) before I deploy my code on my client's web space (goDaddy). I want to note that I have never used PHP before.

我想将用户填写的信息从HTML表单发送到我的电子邮件地址。根据我的理解,由于电子邮件的工作方式,仅使用客户端编码无法做到这一点,并建议使用PHP(结合AJAX)来处理服务器端代码。我在这里按照指南,但我没有收到任何电子邮件地址。在我的客户端网络空间(goDaddy)上部署代码之前,我正在我的机器上进行本地测试(使用XAMPP)。我想要注意,我之前从未使用过PHP。

Javascript:

使用Javascript:

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

PHP (email.php):

PHP(email.php):

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

5 个解决方案

#1


2  

XAMPP doesn't have an e-mail sending thing by itself as default. You may check the links below;

XAMPP默认情况下没有自己发送电子邮件的东西。您可以查看以下链接;

php mail setup in xampp

在xampp中设置php邮件

How do I enable XAMPP to locally use the php's mail() function so I can test my mail() scripts locally without having to upload to my server?

如何让XAMPP在本地使用php的mail()函数,这样我就可以在本地测试我的mail()脚本而无需上传到我的服务器?

#2


2  

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

你应该用$ _REQUEST [“data”]或类似的东西替换$ _REQUEST,因为$ _REQUEST是一个数组。

#3


1  

all time the page is being refreshed. Below is my code:

所有时间页面都被刷新。以下是我的代码:

HTML 5:

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:

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:

<?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';
}
?>

#4


0  

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

This Code shoud be like this

这个代码应该是这样的

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

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

并通过$ _POST ['data']或$ _REQUEST ['data']获取代码

#5


0  

You probably should return false to the form. Try this:

您可能应该将false返回到表单。尝试这个:

$('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;
})

#1


2  

XAMPP doesn't have an e-mail sending thing by itself as default. You may check the links below;

XAMPP默认情况下没有自己发送电子邮件的东西。您可以查看以下链接;

php mail setup in xampp

在xampp中设置php邮件

How do I enable XAMPP to locally use the php's mail() function so I can test my mail() scripts locally without having to upload to my server?

如何让XAMPP在本地使用php的mail()函数,这样我就可以在本地测试我的mail()脚本而无需上传到我的服务器?

#2


2  

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

你应该用$ _REQUEST [“data”]或类似的东西替换$ _REQUEST,因为$ _REQUEST是一个数组。

#3


1  

all time the page is being refreshed. Below is my code:

所有时间页面都被刷新。以下是我的代码:

HTML 5:

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:

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:

<?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';
}
?>

#4


0  

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

This Code shoud be like this

这个代码应该是这样的

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

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

并通过$ _POST ['data']或$ _REQUEST ['data']获取代码

#5


0  

You probably should return false to the form. Try this:

您可能应该将false返回到表单。尝试这个:

$('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;
})