Php 单击“提交”按钮时如何触发邮件功能
我正在尝试制作一个应用程序,在点击按钮时发送电子邮件。我试着使用isset函数来实现这一点。但是我的代码不太好用。我怎么修理它 演示如下:Php 单击“提交”按钮时如何触发邮件功能,php,Php,我正在尝试制作一个应用程序,在点击按钮时发送电子邮件。我试着使用isset函数来实现这一点。但是我的代码不太好用。我怎么修理它 演示如下: <!-- <button name="sendemail">send</button> --> <input type="submit" value="send"/> <?php if(isset($_POST['Submit'])){ ?> <?php // multiple reci
<!-- <button name="sendemail">send</button> -->
<input type="submit" value="send"/>
<?php
if(isset($_POST['Submit'])){
?>
<?php
// multiple recipients
// $to = 'aidan@example.com' . ', ';
// $to .= 'wez@example.com';
$to="example@outlook.com";
// subject
$subject = 'test01';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
?>
您需要为
输入
元素设置名称
属性,并且输入
元素需要是一个表单。此外,$\u POST['submit']
应使用小写字母,以保持一致性
您还应该检查mail()
是否成功发送mail()
发送时将返回true
,未发送时返回false
只需注意,邮件的实际发送将取决于您的主机/服务器,即使返回true
,也不一定意味着邮件已发送
有关mail()
的更多信息,请访问
以下代码应该可以工作:
<form method="post">
<input type="submit" name="submit" value="send" />
</form>
<?php
if(isset($_POST['submit'])){
// NO Change from your original code
if(mail($to, $subject, $message, $headers)) {
// You can change this to whatever you want PHP to do when mail is successfully sent
echo 'Mail successfully sent';
} else {
// You can change this to whatever you want PHP to do when mail failed to send
echo 'Mail failed to send';
}
?>
希望这有帮助。谢谢 单击“发送电子邮件”按钮,您可以使用以下代码发送电子邮件
<form action="" method="post">
<input type="submit" value="Send details to college" />
<input type="hidden" name="button_pressed" value="1" />
</form>
<?php
if(isset($_POST['button_pressed']))
{
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
这里有几个问题需要解决。首先,您的输入按钮不起任何作用,因为它不是表单的一部分。因此,您要么使用某种JavaScript,要么只使用HTML表单来提交按钮,这样我们就可以在PHP中处理它。下面是一个简单的表单。注意
metohd=“POST”
,这允许我们稍后从PHP中的$\u POST
-数组中收集信息(默认情况下,表单设置为使用GET)
您会遇到什么错误?提交按钮没有
name
属性,并且按钮是否放置在表单中(意味着它首先通过POST发送)?另外,这个?>@Qirel和PHP结束标记?>是一个简单的bad@VladimirHraban……嗯?您的意思是什么?@Qirel在代码后面包含结束标记是一种不好的做法,因为?>之后可能会出现意外的空白。并将它们输出给用户(例如,导致标头已发送警告)
<form method="POST">
<input type="submit" name="submit" value="Send!" />
</form>
<?php
if (isset($_POST['submit'])) {
/* add headers, messages and such here */
// Check if the mail is being sent from the server
if (mail($to, $subject, $message, $headers)) [
// Mail was sent! Great!
} else {
// It failed sending the mail
}
}
?>