简体   繁体   中英

PHP mailer redirect issue

I came across an issue using PHPmailer that I just don't understand. I have an assist function redirect for the header function that works 100% in the position shown. It fails to work in the positions shown in the comments where I would like it to work. Everything else works perfectly regardless of where the redirect function is placed. Any ideas? This is also my first post so apologies in advance if there is already a solution which I couldn't find.

<?php

if(isset($_POST['replyall'])) {

    redirect("index.php?leadreply");   // Why does this have to be here to work???? //       

    if(isset($_POST['chk1'])) { 

        $email = new PHPMailer();
        $email->From      = $_POST['author'];
        $email->FromName  = 'JGM Decorating';
        $email->Subject   = 'Reply to your contact request';
        $email->Body      = $_POST['comments'];
        $email->AddAddress( $_POST['destination'] );

        $file_to_attach = '../crm/gtcjgm.pdf';

        $email->AddAttachment( $file_to_attach , 'Terms and Conditions.pdf' );

        return $email->Send();

        // I would like to have the redirect here but it doesn't work??//

     } else {

        $email = new PHPMailer();
        $email->From      = $_POST['author'];
        $email->FromName  = 'JGM Decorating';
        $email->Subject   = 'Reply to your contact request';
        $email->Body      = $_POST['comments'];
        $email->AddAddress( $_POST['destination'] );

        $file_to_attach = '';

        $email->AddAttachment( $file_to_attach , 'Terms and Conditions.pdf' );    

        return $email->Send(); 

        // I would like to have a different redirect here but it doesn't work??//      

    }
}
?>

This code:

return $email->Send();

Explanation: This will be returning the function and it will stop the process at this stage itself and after that whatever line you place it will not work. (Eg) Echo statement also will not work.

If you want to do both you have to do the mail sending process using Ajax and after that alone you have to redirect as per you need .

Since the return will stop the execution at that instant itself. So in order to do so you can use it with the hep of AJAX and then redirect it.

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