PHP PEAR Mail 发送了 2 封邮件

PHP PEAR Mail sends 2 emails

此脚本有效,但在访问时发送空白电子邮件。然后提交时是真实的。我想摆脱它发送的第一个空白。这主要是我在此处和另一个地方找到的经过编辑的代码,我拼凑在一起以满足我的需要。

<?php
ini_set("include_path", '/home/user/php:' . ini_get("include_path") );
require_once "Mail.php";

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$contact = $_POST['contact'];
$comments = $_POST['comments'];
$from = "Info <info@domain.net>";
$to = "Info <info@domain.net>";
$subject = "Customer Contact Form";
$body = "If this works it will be edited a bit \n" . "First Name: " . $first_name . "\n" . "Last Name: " . $last_name . "\n" . "Email: " . $email . "\n" . "Address: " . $address . "\n" . "Phone: " . $phone . "\n" . "Please contact me by: " . $contact . "\n" . "Other Comments: " . $comments;

$host = "mail.domain.net";
$username = "info@domain.net";
$password = "emailpassword";

$headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
$smtp = Mail::factory('smtp',
    array ('host' => $host,
        'auth' => true,
        'username' => $username,
        'password' => $password));

$mail = $smtp->send($to, $headers, $body);

//if (PEAR::isError($mail)) {
//  echo("<p>" . $mail->getMessage() . "</p>");
//} else {
//  echo("<p>Message successfully sent!</p>");
//}
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Address: <input type="text" name="address"><br>
Phone: <input type="text" name="phone"><br>
Perferred Form of Contact: <input type="text" name="contact"><br> 
Message:<br><textarea rows="5" name="comments" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

因为你没有检查表单数据! 只需在您的代码周围使用一个简单的 if(isset($_POST['first_name'])) 来生成和发送邮件,如果您发送了表单,它就会发送一封电子邮件。