我希望通过联系表单将电子邮件发送到我的电子邮件地址。客户将在表单中输入他们的姓名,电子邮件地址和消息。但是,当电子邮件发送到我的地址时,我只能看到用户meessage。 我怎样才能使客户名称,电子邮件地址和消息显示在电子邮件中。
if (empty($_POST) === false) {
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) === true || empty($email) === true || empty($message) === true) {
$errors [] = 'Name ,email and message are required !';
} else {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'That\'s not a valid email address';
}
if (ctype_alpha ($name) === false) {
$errors[] = 'Name must only contain Letters';
}
}
if (empty($errors) === true) {
mail('someone@hotmail.com', 'Contact form', $message, 'From: ' . $email);
header('Location: index.php?sent');
exit();
}
}
<!DOCTYPE html>
<html>
<head>
<title>A contact form</title>
</head>
<body>
<?php
if (isset($_GET['sent']) === true) {
echo '<p>Thanks for Contacting us !</p>';
} else {
if (empty($errors) === false) {
echo '<ul>';
foreach ($errors as $error) {
echo '<li>', $error, '</li>' ;
}
echo'</ul>';
}
?>
<form action="" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name" <?php if (isset($_POST['name']) === true) {echo 'value="', strip_tags($_POST['name']), '"'; } ?>>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email" <?php if (isset($_POST['email']) === true) {echo 'value="', strip_tags($_POST['email']), '"'; } ?>>
</p>
<p>
<label for="message">Message</label><br>
<textarea name="message" id="message"><?php if (isset($_POST['message']) === true) {echo strip_tags($_POST['message']); } ?></textarea>
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
<?php
}
?>
</body>
</html>
答案 0 :(得分:0)
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$message = $message. " Email: $email". " Name: $name";
您可以将消息参数编辑为更长并包含更多信息。你甚至可以将它设置为html。
答案 1 :(得分:0)
根据php mail()手册:
{ "query":{ "bool":{ "should":[ { "multi_match": { "query":"searchquery", "operator":"and", "fields":["Name", "Description", "Text"] } }, { "bool":{ "must": [ {"terms": {"Id": [1, 2]}}, {"term": {"_type": "Project"}} ] } } ], "filter": [ {"terms": {"ModuleId": [9, 5]}} ] } } }
:接收者或邮件的接收者。此字符串的格式必须符合»RFC 2822。一些 例子是:
to
user@example.com
user@example.com, anotheruser@example.com
User <user@example.com>
User <user@example.com>, Another User <anotheruser@example.com>
说明:
mail()
示例1:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
示例2:
<?php
mail("john doe <nobody@example.com>", "my subject", "my message")
?>
答案 2 :(得分:0)
只需添加名称,然后通过电子邮件发送邮件即可。我们一直使用这种方法使用PHP邮件。请参阅下面的$newMessage
变量。它包含名称和电子邮件添加到其顶部的消息,以便于参考。
如果您将$headers
变量创建为数组或字符串,具体取决于您将拥有多少其他参数,也可以更轻松地查看您正在执行的操作。我在这里完成的方式你会看到&#34; From&#34;电子邮件的一部分。
<?php
if (empty($_POST) === false) {
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) === true || empty($email) === true || empty($message) === true) {
$errors [] = 'Name ,email and message are required !';
} else {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'That\'s not a valid email address';
}
if (ctype_alpha ($name) === false) {
$errors[] = 'Name must only contain Letters';
}
}
if (empty($errors) === true) {
$headers = 'From: ' . $name . ' <' . $email . '>';
// Create the new message to send with the name and email.
$newMessage = 'Name: ' . $name . '<br/>';
$newMessage .= 'Email: ' . $email . '<br/>';
$newMessage .= $message;
mail('someone@hotmail.com', 'Contact form', $newMessage, $headers);
header('Location: index.php?sent');
exit();
}
}
?>