我希望通過聯繫表單將電子郵件發送到我的電子郵件地址。在表單上,客戶將輸入他們的姓名,電子郵件地址和消息。但是,當電子郵件發送到我的地址時,我可以只能看到用戶的消息。 我該怎麼做才能讓客戶姓名,電子郵件地址和郵件顯示在電子郵件中。使用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) {
mail('[email protected]', '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>
應如何顯示姓名,地址和信息?像「來自:Peter Parker [email protected]」?請更具體地說明你想實現的目標。 – elementzero23
正確放入參數並將附加值附加到$ message字符串。有關發送信息請參閱http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php – Blackbam
首先閱讀'mail()'文檔 - http://php.net/manual /en/function.mail.php –