1

This script works, but it sends a blank email when it is visited. Then a real one when submitted. I want to get rid of the first blank one it sends. This is mostly edited code I found here and another place I pieced together to work for my needs.

<?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>
Shane Becker
  • 41
  • 1
  • 1
  • 11

1 Answers1

1

Because you don't check the form data! Just use a simple if(isset($_POST['first_name'])) around your code for generating and sending the mail and it will just send an email if you've sent the form.

csabinho
  • 1,579
  • 1
  • 18
  • 28
  • Thank you. In case anyone else wants to know, his code goes goes before $headers with a [ and another ] right before the stuff that is commented out. – Shane Becker Dec 30 '17 at 01:47