我一直试图制作一个php表单,因此用户可以发送电子邮件。但由于我目前缺乏PHP知识,这有点难以解决。所以我已经收集了一些完成的代码片段,但它仍然不起作用。
这是我在名为" submit.php"
的文件中的PHP代码@users_not_rated = User.find_by_sql("
SELECT pp.product_id AS father_id, p.product_id, u.user_id FROM products p
INNER JOIN products pp ON p.parent_id = pp.product_id
INNER JOIN order_products op ON p.product_id = op.product_id
INNER JOIN orders o ON op.order_id = o.order_id
INNER JOIN users u ON o.user_id = u.user_id
WHERE NOT EXISTS (
SELECT r.user_id, r.product_id FROM ratings r
WHERE r.user_id = u.user_id
AND r.product_id = p.product_id
)
")
继承人" form.php"我在
中有html表单<?php
if(isset($_POST['submit'])){
$to = "example@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
还有人告诉我表格没有安全保障, &#34; 因为它不会删除html标记&#34;
如何解决这个问题?
答案 0 :(得分:1)
您的表单未提交给PHP脚本
更改
<form action="" method="POST">
到
<form action="submit.php" method="POST">
修改:我发现你包括submit.php
,所以你可能想要这样做
<form action="form.php" method="POST">
答案 1 :(得分:0)
您的服务器应配置Sendmail或其他邮件服务器。
要剥离HTML,您可以使用$message = strip_tags($message); $message2 = strip_tags($message2);
或者您可以使用htmlspecialchars
将特殊字符转换为HTML实体。
答案 2 :(得分:0)
这种语法多年来一直适用于我:
在"From: $from<>\n";
中,<>
是通常名称所在的位置。即使空白也留在那里。
$mailheaders = "From: $from<>\n";
$mailheaders .= "Reply-To: No1@noone.com\n";
mail($to,$subject, $message, $mailheaders, "-fmailer@yoursite.com");
答案 3 :(得分:0)
此外,除了添加错误报告和服务器配置外,您的标头还需要其他属性。否则,如果发送,电子邮件将以垃圾邮件结束。请考虑添加以下标题信息。
/* well formed hearder */
$headers = 'From: '.$senderemail."\r\n";
$headers .= 'Reply-To: '.$senderemail."\r\n";
$headers .= 'Return-Path: '.$senderemail."\r\n";
/* additional header */
$headers .= 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-Type: text/plain;'."\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion()."\r\n";
最后,您可以在$ to属性中使用逗号分隔列表。因此,不是两次运行mail(),而是将地址连接到地址。
$to = "john_doe@example.com,example@example.com";