0

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

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • 1
    Which one works? `$headers` is never used. The native PHP mail function has poor error reporting I recommend Swift or PHPMailer. – user3783243 May 03 '20 at 04:45

1 Answers1

0

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);
}
Abed Putra
  • 1,153
  • 2
  • 17
  • 37