PHP PEAR Mail发送2封电子邮件

时间:2017-12-30 01:09:29

标签: php html email pear

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

<?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>

1 个答案:

答案 0 :(得分:1)

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