2016-03-26 65 views
-1

我想讓PHPMailer工作從表單數據(顯然)發送郵件。PHPMailer發送到空白頁

問題:

當提交表單,我收到發送到一個空白頁。沒有錯誤報告。我已經按照我所能找到的圍繞此問題的許多計算器線程進行了處理,並且這些線程中沒有任何解決方案可以解決這個問題。

感謝

錯誤:

從修正所有錯誤的旁白:2016年3月26日22時10分43秒SMTP錯誤:無法連接到服務器:連接被拒絕(111)2016年3月26日22:10:43 SMTP連接()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

PHP:

<?php 
$message= 
'First Name: '.$_POST['first'].'<br /> 
Last Name: '.$_POST['last'].'<br /> 
Phone: '.$_POST['phone'].'<br /> 
Email: '.$_POST['email'].'<br /> 
Message: '.$_POST['message'].' 
'; 

require 'PHPMailer/PHPMailerAutoload.php'; 

    $mail = new PHPMailer(); 

    $mail->SMTPDebug = 2;   // Enable verbose debug output 
    $mail->IsSMTP();    // Sets up a SMTP connection 
    $mail->CharSet = 'UTF-8'; 
    $mail->SMTPAuth = true;   // Connection with the SMTP does require authorization  
    $mail->SMTPSecure = "tls";  // Connect using a TLS connection 
    $mail->Host = "smtp.gmail.com"; //Gmail SMTP server address 
    $mail->Port = 587; 

    // Authentication 
    $mail->Username = "[email protected]"; // Your full Gmail address 
    $mail->Password = "xxxx"; // Your Gmail password 

    // Compose 
    $mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']); 
    $mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']); 
    $mail->Subject = "New Contact Form Enquiry";  // Subject (which isn't required) 
    $mail->MsgHTML($message); 

    // Send To 
    $mail->AddAddress("[email protected]", "xxxx"); // Where to send it - Recipient 
    $result = $mail->Send();  // Send! 
    $message = $result ? 'Successfully Sent!' : 'Sending Failed!';  
    unset($mail); 
} 
?> 

HTML:

<form class="form-inline" name="contactform" action="mailer.php" enctype="multipart/form-data" method="POST"> 
        <div class="col-sm-6 form-group form-sty"> 
         <label class="sr-only" for="first" required>First Name</label> 
         <input type="text" class="form-control-sty" name="first" placeholder="First Name"> 
        </div> 
        <div class="col-sm-6 form-group form-sty"> 
         <label class="sr-only" for="last" required>Last Name</label> 
         <input type="text" class="form-control-sty" name="last" placeholder="Last Name"> 
        </div> 
        <div class="col-sm-6 form-group form-sty"> 
         <label class="sr-only" for="phone" required>Phone</label> 
         <input type="text" class="form-control-sty" name="phone" placeholder="Phone"> 
        </div> 
        <div class="col-sm-6 form-group form-sty"> 
         <label class="sr-only" for="email" required>Email</label> 
         <input type="email" class="form-control-sty" name="mailfrom" placeholder="Email"> 
        </div> 
        <div class="col-sm-12 form-sty"> 
         <label class="sr-only" for="comment">Comment</label> 
         <textarea class="form-control-sty" name="message" id="message" rows="7" placeholder="What's on your mind?" required></textarea> 
        </div> 
        <div class="col-sm-12 form-sty form-sty-btn"> 
         <button type="submit" value="Submit" class="btn btn-default col-sm-12">Send</button> 
        </div> 
        <p><?php if(!empty($message)) echo $message; ?></p> 
       </form> 
+1

你說你有一個問題,然後不說什麼問題*是*。你得到什麼錯誤/警告?如果您只是發佈格式不正確的代碼的牆,我們如何幫助您? –

+0

請放棄請求和編輯。只需添加相關的代碼並清楚地說明問題。 – j08691

+0

對不起,它在標題中。問題在於,PHPMailer在提交後將我發送到空白頁面。 我沒有收到任何錯誤,只是進入了空白頁面。 – dbrree

回答

1

您需要的標題重定向回的形式(我認爲是你想送他們)與您的網址更換水箱內的位置

<?php 
$message= 
'First Name: '.$_POST['first'].'<br /> 
Last Name: '.$_POST['last'].'<br /> 
Phone: '.$_POST['phone'].'<br /> 
Email: '.$_POST['email'].'<br /> 
Message: '.$_POST['message'].' 
'; 

require 'PHPMailer/PHPMailerAutoload.php'; 

    $mail = new PHPMailer(); 

    $mail->SMTPDebug = 2;   // Enable verbose debug output 
    $mail->IsSMTP();    // Sets up a SMTP connection 
    $mail->CharSet = 'UTF-8'; 
    $mail->SMTPAuth = true;   // Connection with the SMTP does require authorization  
    $mail->SMTPSecure = "tls";  // Connect using a TLS connection 
    $mail->Host = "smtp.gmail.com"; //Gmail SMTP server address 
    $mail->Port = 587; 

    // Authentication 
    $mail->Username = "[email protected]"; // Your full Gmail address 
    $mail->Password = "xxxx"; // Your Gmail password 

    // Compose 
    $mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']); 
    $mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']); 
    $mail->Subject = "New Contact Form Enquiry";  // Subject (which isn't required) 
    $mail->MsgHTML($message); 

    // Send To 
    $mail->AddAddress("[email protected]", "xxxx"); // Where to send it - Recipient 
    $result = $mail->Send();  // Send! 
    if ($result) 
    { 
     header("Location: : http://www.example.com/contactform.php"); 
    } 
    else { 
     echo "sending mail failed: " . $mail->ErrorInfo; 
    } 
    unset($mail); 
} 
?> 
+0

這實際上給了我以下錯誤: 警告:無法修改頭信息 - 已在/ home/xxxx /中發送的頭文件(輸出開始於/home/xxxx/public_html/PHPMailer/class.smtp.php:234)第41行的public_html/mailer.php – dbrree

+1

在設置標題之前,您不能回顯任何輸出。所以確保頭部之前沒有調試回聲。 – Torchify

+0

也解決了這個問題。我必須禁用/註釋掉SMTPDebug,它工作得很好。 感謝您的幫助。 – dbrree

1

你應該確保沒有錯誤。嘗試將此代碼放在文件的頂部,以確保報告了所有錯誤/警告並再次運行腳本。

ini_set('display_startup_errors',1); 
ini_set('display_errors',1); 
error_reporting(E_ALL); 
+0

謝謝,我現在能夠看到錯誤。如果我無法弄清楚會發布。 – dbrree

+0

我在OP中添加了錯誤。 – dbrree

+0

好吧我修復了所有錯誤:2016-03-26 22:10:43 \t SMTP錯誤:無法連接到服務器:連接被拒絕(111)2016-03-26 22:10:43 \t SMTP connect()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – dbrree