这个联系表格是否完全有效?(Is this contact form fully function?)

编程入门 行业动态 更新时间:2024-10-28 14:25:14
这个联系表格是否完全有效?(Is this contact form fully function?)

填写表格时,表示发送成功。 所以我需要启用或包含phpmailer? 表单通过submit.php发送,但联系表单是一个html页面。 我是否也需要将联系页面设为php。 submit.php的chmod权限是755.在脚本中我确实有正确的电子邮件地址。 我安装了phpmailer,是否需要激活它?

<?php // specify your email here $to = 'example@example.com'; // Assigning data from $_POST array to variables if (isset($_POST['name'])) { $name = $_POST['name']; } if (isset($_POST['email'])) { $from = $_POST['email']; } if (isset($_POST['company'])) { $company = $_POST['company']; } if (isset($_POST['message'])) { $message = $_POST['message']; } // Construct subject of the email $subject = 'Contact Inquery ' . $name; // Construct email body $body_message .= 'NAME: ' . $name . "\r\n\n"; $body_message .= 'EMAIL: ' . $from . "\r\n\n"; $body_message .= 'SUBJECT: ' . $company . "\r\n\n"; $body_message .= 'MESSAGE: ' . $message . "\r\n\n"; // Construct headers of the message $headers = 'From: ' . $from . "\r\n"; $headers .= 'Reply-To: ' . $from . "\r\n"; $mail_sent = mail($to, $subject, $body_message, $headers); if ($mail_sent == true) { ?> <script language="javascript" type="text/javascript"> window.alert("Sent Successfuly."); </script> <?php } else { ?> <script language="javascript" type="text/javascript"> window.alert("Error! Please Try Again Later."); </script> <?php } // End else ?>

When filling out the form it is saying sent successfully. So I need to enable or include phpmailer? The form is being sent via submit.php but the contact form is an html page. Do I also need to make the contact page php as well. Chmod permissions for submit.php is 755. In the script I do have the email address properly. I have phpmailer installed, do I need to activate it?

<?php // specify your email here $to = 'example@example.com'; // Assigning data from $_POST array to variables if (isset($_POST['name'])) { $name = $_POST['name']; } if (isset($_POST['email'])) { $from = $_POST['email']; } if (isset($_POST['company'])) { $company = $_POST['company']; } if (isset($_POST['message'])) { $message = $_POST['message']; } // Construct subject of the email $subject = 'Contact Inquery ' . $name; // Construct email body $body_message .= 'NAME: ' . $name . "\r\n\n"; $body_message .= 'EMAIL: ' . $from . "\r\n\n"; $body_message .= 'SUBJECT: ' . $company . "\r\n\n"; $body_message .= 'MESSAGE: ' . $message . "\r\n\n"; // Construct headers of the message $headers = 'From: ' . $from . "\r\n"; $headers .= 'Reply-To: ' . $from . "\r\n"; $mail_sent = mail($to, $subject, $body_message, $headers); if ($mail_sent == true) { ?> <script language="javascript" type="text/javascript"> window.alert("Sent Successfuly."); </script> <?php } else { ?> <script language="javascript" type="text/javascript"> window.alert("Error! Please Try Again Later."); </script> <?php } // End else ?>

最满意答案

目前,您的表单不需要由PHP呈现。 因此,它可以保存在HTML文件中。

当你说:

表单通过submit.php发送

我想你想说:

该表单将发送 submit.php

如果是这种情况,那么我认为您发布的源代码是此文件的内容。

关于电子邮件发送,您使用内置的PHP mail()函数。 但是此函数使用需要在服务器上安装的sendmail二进制文件 。 此外,您必须在php.ini文件中设置适当的设置 ,以使您的主机正确发送电子邮件。 PHPMailer是一个很好的替代品,因为它不需要任何外部依赖。 这个简单的例子将向您展示如何使用它。

注意:我看到你的PHP代码非常简单,我推断你是一个初学者,你把这个源代码带到了某个地方并且你并不真正理解它。 那就对了。 你必须从某个地方开始。

但:

小心这段代码! 它接受用户输入并在没有任何健全性检查的情况下发送它。 我建议您不要在生产环境中部署此代码 (如果它是用于测试和学习,那是正确的)。

看看这个关于PHP用户输入卫生的答案 。 它没有谈论电子邮件发送的卫生设施。 但它可能是一个很好的起点,了解它是如何工作的基本。 然后你必须自己搜索。

Currently, your form doesn't needs to be rendered by PHP. So, it cans be kept in an HTML file.

When you say:

The form is being sent via submit.php

I think you want to say:

The form is being sent to submit.php

If this is the case, then I think the source code you posted is the content of this file.

Concerning the email sending, you use the built-in PHP mail() function. But this function use the sendmail binary which needs to be installed on your server. Moreover, you have to setup the appropriate settings in the php.ini file in order to make your host correctly send the email. PHPMailer is a good replacement as it doesn't require any external dependencies. This simple example will show you basically how to use it.

N.B: I see that you PHP code is very simple and I deduce that you're a beginner, that you took this source code somewhere and that you don't really understand it. That's right. You have to start from somewhere.

BUT:

Be careful with this code! It takes the user inputs and sends it without any sanity checks. I recommend you to not deploy this code in production environment (if it's for testing and learning, that's right).

Look at this answer about PHP user input sanitation. It doesn't talk about sanitation for email sending. But it might be a good starting point to understand how it works basically. Then you have to search by yourself.

更多推荐

php,phpmailer,email,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-04-27 12:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   contact   form   fully   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!