PHP SMTP Mail Contact form

2019-09-07 22:02发布

问题:

I have a contact form using SMTP authentication using Pear. I want to send an acknowledgment email to the person completing the form as well.

What would I have to add to my php to make this happen

<?php
        error_reporting(E_ALL);
        ini_set('display_errors', True);

        set_include_path('******');

        require_once "Mail.php";

// Set the email variables from the form here:

    $name = $_POST['name']; // Pass the Name value to the $Name variable
    $number = $_POST['number'];
    $email = $_POST['email']; // Pass the Email value to the $Email variable
    $enquiry = $_POST['enquiry'];
    $comment = $_POST['message']; // Pass the Comment value to the $Comment variable

 $from = $email;
 $to = "Enquiries <enquiries@brisbanediveacademy.com.au>";
 $subject = "Enquiry ( $enquiry)";

        $body = "You have received a Web Enquiry. \n
            Name: $name\n
            Contact: $number \n
            Email: $email\n
            Enquiry Type: $enquiry \n
            Comments: $comment";

 $host = "******.com.au";
 $username = "*****.com.au";
 $password = "******";

 $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! We will be in contact with you shortly!</p>");
 }
 ?>

Thanks,

回答1:

You can use CC in headers array

 'Cc' => 'Sendername <$from>',

or eve beter

'Bcc' => 'Sendername <$from>',


回答2:

Ivan's solution is good if you want them to receive the same email you get.

But best is to simply send him a different email.

You generate two. Once for you and one for him.

If you want to have common parts use a separate variable for that and just append or prepend to the message.



标签: php smtp