PHP如果邮件发送,转到成功页面,如果没有转到失败页面

时间:2016-02-12 14:07:55

标签: php email phpmailer

首先,感谢所有过去帮助过问题的人 我有一个PHP网页,它将一个申请表提交给一个电子邮件地址,最后我有一个IF功能,如果电子邮件发送,则将用户发送到success.html,如果没有,则发送error.html。

无论电子邮件是否发送,用户始终会被带到error.html

我的代码如下:

public class App implements SessionManager {
    void launchModule1(){
        Module1.startModule(this);
    }
    void onTaskFinished(){
        Module2.start();
    }

我在点击按钮时使用PHPMailer发送电子邮件 我遇到问题的部分如下:

    require '/PHPMailer-master/PHPMailerAutoload.php';
    require '/PHPMailer-master/class.phpmailer.php';

    $mail = new PHPMailer;

    if(isset($_POST['submit'])){
        $to = "me@mymail.com";
        $from = $_POST['text_96'];
        $fullname = $_POST['text_95'];
        $email = $_POST['text_96'];
        $dob = $_POST['date_12'];
        $addr = $_POST['textarea_31'];
        $maths = $_POST['text_93'];
        $english = $_POST['text_31'];
        $holidayst = $_POST['date_74'];
        $holidayend = $_POST['date_91'];
        $distance = $_POST['number_58'];
        $awayhome = $_POST['selectlist_90'];

        foreach($_POST['checkbox'] as $key=>$val){$apprent .= $val.' ';}
        //$fileatt = $_POST['file'];
        $reasontext = $_POST['textarea_60'];
        $message = "Message";

        $mail->setFrom($from, $fullname);
        $mail->addAddress('me@mymail.com');
        $mail->addAttachment($_FILES['file']['name']); 

        if (isset($_FILES['file']) &&
        $_FILES['file']['error'] == UPLOAD_ERR_OK) {
        $mail->AddAttachment($_FILES['file']['tmp_name'],
                     $_FILES['file']['name']);
        }

        $mail->Subject = 'subject';
        $mail->Body    = $message;

    if(!$mail->send()) {
        $message = NULL;
        $headers = NULL;
        header('Location: success.html');
    } else {
        $message = NULL;
        $headers = NULL;
        header('Location: error.html');
    }

我希望这是有道理的,如果您需要任何进一步的信息,请告诉我 任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

不应该是:

if($mail->send()) {
        $message = NULL;
        $headers = NULL;
        header('Location: success.html');
    } else {
        $message = NULL;
        $headers = NULL;
        header('Location: error.html');
    }

$ mail-> send()在成功的情况下返回true,而不是相反。

答案 1 :(得分:1)

根据PHPMailer(https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php)的源代码,send()方法将在出错时返回false,如果成功发送则返回true。查看代码,当send()返回false

时,您将用户发送到success.html页面

在您的代码更改中:

if(!$mail->send()) {
    $message = NULL;
    $headers = NULL;
    header('Location: success.html');

To(即删除“!”)

if($mail->send()) {
    $message = NULL;
    $headers = NULL;
    header('Location: success.html');

你还应该在try {} catch {}块中包含对send()的调用,以捕获它可能抛出的任何异常,这样你仍然可以控制将用户发送到你的错误页面。

答案 2 :(得分:0)

这应该适合你。

require '/PHPMailer-master/PHPMailerAutoload.php';
require '/PHPMailer-master/class.phpmailer.php';

$mail = new PHPMailer(true);

if(isset($_POST['submit'])){
    $to = "me@mymail.com";
    $from = $_POST['text_96'];
    $fullname = $_POST['text_95'];
    $email = $_POST['text_96'];
    $dob = $_POST['date_12'];
    $addr = $_POST['textarea_31'];
    $maths = $_POST['text_93'];
    $english = $_POST['text_31'];
    $holidayst = $_POST['date_74'];
    $holidayend = $_POST['date_91'];
    $distance = $_POST['number_58'];
    $awayhome = $_POST['selectlist_90'];

    foreach($_POST['checkbox'] as $key=>$val){$apprent .= $val.' ';}
    //$fileatt = $_POST['file'];
    $reasontext = $_POST['textarea_60'];
    $message = "Message";

    $mail->setFrom($from, $fullname);
    $mail->addAddress('me@mymail.com');
    $mail->addAttachment($_FILES['file']['name']); 

    if (isset($_FILES['file']) &&
    $_FILES['file']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['file']['tmp_name'],
                 $_FILES['file']['name']);
    }

    $mail->Subject = 'subject';
    $mail->Body    = $message;
    try{
        $mail->send()) {
        header('Location: success.html');
    }catch(Exception $e)
    {
        header('Location: error.html');
    }
    finally{
        $message = NULL;
        $headers = NULL;
    }
}