简体   繁体   中英

PHP Confirmation Email after filling out Contact Form

I've created a contact page for a project im working on and im having trouble getting it to send a confirmation email to the user. I have the function defined on my emailer page along with the code that sends the contact form to me

public function sendEmail(){ //this will format and send an email to the smtp server
        $to = $this->getSenderEmail();
        $subject = $this->getSubject();
        $message = $this->getMessage();
        $headers = "From: <contact@tristonnearmyer.com >";
        $from = $this->getCustomerInfo();
        //it will use the php mail()
        return mail($to,$subject,$message,$from);
}
public function sendConfEmail(){
        $userEmail = $this->getRecipientEmail();
        $confSubject = "confirmation";
        $confMessage = "thank you";
        return mail($userEmail, $confSubject, $confMessage);
}

then the functions are used on the contact page

echo $emailTest->sendEmail(); //send email to SMTP server
echo $emailTest->sendConfEmail(); //send confirmation email

I can't seem to figure out why one is working while the other isn't

Please try this code, you need to call the function sendConfEmail after sendEmail success. Also from here you need to modify the header. But base on user3783243 commend better change to PHPMailer or use swift

public function sendEmail(){ //this will format and send an email to the smtp server
    $to = $this->getSenderEmail();
    $subject = $this->getSubject();
    $message = $this->getMessage();
    $from = $this->getCustomerInfo();
    $headers = 'From: '. $from . "\r\n" .
        'Reply-To: youremailhere@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();


    $sendMail =  mail($to,$subject,$message,$headers);

    if($sendMail){
        // Call the function send confirmation
        $this->sendConfEmail();
    }else{
        echo 'failed sent the email';
    }

}

public function sendConfEmail(){
    $userEmail = $this->getRecipientEmail();
    $confSubject = "confirmation";
    $confMessage = "thank you";
    $from = $this->getCustomerInfo();
    $headers = 'From: '. $from . "\r\n" .
        'Reply-To: youremailhere@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    return mail($userEmail, $confSubject, $confMessage,$headers);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM