Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

onproae's avatar

PHP Mailer Auto response

Hi,

Can someone please help me with how to fix my problem on PHP, I created a contact form and when a client will submit the form I want them to receive an autoresponder.

This is my code.

use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php';

    $email = $_REQUEST ['email'];

// Instantiation and passing true enables exceptions $mail = new PHPMailer(true);

    //Server settings
    //$mail->SMTPDebug = 1;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host = 'smtp.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth = true;                                   // Enable SMTP authentication
    $mail->Username = '';                     // SMTP username
    $mail->Password = '';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port = 587;                                   // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    $mail ->From = $email;

    $mail->addAddress('[email protected]', 'Kristian User');

    // set word wrap to 50 characters
    $mail->WordWrap = 50;
    // set email format to HTML
    $mail->IsHTML(true);

    $mail->Subject = "OnPro Web New Subscription";

    $message = 'Company Name = ' . $_POST['company_name'] . '<br>' . 'Name = ' . $_POST['fname'] . '<br>' . 'Email = ' . $_POST['email'] . '<br>' . 'Mobile Number = ' . $_POST['mobile_number'] . '<br>' . 'Number of Employees = ' . $_POST['number_employees'];
    $mail->Body = $message;
    $mail->AltBody = $message;

    if(!$mail->send())
    {
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }
    echo "Message has been sent";

    $mail2 = new PHPMailer();

    // set mailer to use SMTP
    $mail2->IsSMTP();

    $mail->Username = '[email protected]';                     // SMTP username
    $mail->Password = 'Maryjen0312!';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port = 587;                                   // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above


    $mail2->From = $_POST['email'];

    // below we want to set the email address we will be sending our email to.
    $mail2->addAddress($_POST['email']);

    // set word wrap to 50 characters
    $mail2->WordWrap = 50;
    // set email format to HTML
    $mail2->IsHTML(true);

    $mail2->Subject = "Thank you for joining";

    $message = "Please stay tune for updates";
    $message = 'Dear ' . $_POST['fname'];
    $mail2->Body = $message;
    $mail2->AltBody = $message;

    if(!$mail2->send())
    {
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail2->ErrorInfo;
        exit;
    }

    echo "Message has been sent";

Thank you for your help in advance.

0 likes
1 reply
drewdan's avatar

Probably a good idea to edit this to remove your sensitive data, ie. email and password!

As an aside, I am not quite sure what you are asking? Does this not work, is there an error?

Please or to participate in this conversation.