2016-08-19 75 views
0

我提交表單後發送電子郵件時遇到麻煩,此消息顯示:「已發送消息」,但我沒有收到任何電子郵件,而我甚至檢查了垃圾郵件文件夾及其空白。用PHPMailer通過聯繫表發送電子郵件時遇到麻煩

下面是HTML表單代碼:

<form action="<?php echo $PHP_SELF; ?>" method="post"> 

<input type="text" class="form-control" placeholder="Name" name="name"> 

<input type="email" class="form-control" placeholder="Email" name="email"> 

<input type="text" class="form-control" placeholder="Subject" name="subject"> 

<textarea class="form-control" placeholder="Message" rows="7" name="message"> </textarea> 

<input type="submit" class="form-control" name="submit" value="submit"> 

</form> 

和PHP代碼爲:

<?php 
if(isset($_POST['email'])) { 

    if(isset($_POST['subject'])){ $subject = $_POST['subject']; } 
    if(isset($_POST['message'])){ $message = $_POST['message']; } 
    if(isset($_POST['name'])){ $name = $_POST['name']; } 
    if(isset($_POST['email'])){ $email = $_POST['email']; } 

require_once('class.phpmailer.php'); 

$mail    = new PHPMailer(); 

$body    = $message; 

$mail->SetFrom($email, $name); 

$address = "[email protected]"; 
$mail->AddAddress($address, "[email protected]"); 

$mail->Subject = $subject; 

$mail->MsgHTML($body); 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
} 

?> 
+3

我建議你檢查你的SMTP配置。你有一個有效的SMTP設置的電子郵件服務器?從您的代碼中,您沒有使用任何SMTP服務器發送電子郵件。所以即使您的代碼正確並且返回成功,郵件也不會被髮送。 – d3r1ck

+0

當未配置(無SMTP中繼)時,PHPMailer使用與「mail()」函數相同的傳輸。因此適用相同的條件。啓用調試,檢查日誌,DKIM/SPF等等等。 – mario

+0

感謝您的回答和建議,我編輯了php代碼並添加了smtp配置,但smtp auth禁用導致域不需要身份驗證,但問題仍然是同樣...... –

回答

0

這裏是你的HTML代碼

<form action="yourphpcode.php" method="post"> 

<input type="text" class="form-control" placeholder="Name" name="name"> 

<input type="email" class="form-control" placeholder="Email" name="email"> 

<input type="text" class="form-control" placeholder="Subject" name="subject"> 

<textarea class="form-control" placeholder="Message" rows="7" name="message"> </textarea> 

<input type="submit" class="form-control" name="submit" value="submit"> 

</form> 

這裏是你的PHP代碼

<?php 
if (isset($_POST['submit'])) { 


    $subject = "subject : " . $_POST['subject'] . "<br>"; 
    $message = "message :" . $_POST['message'] . "<br>"; 
    $name = "name :" . $_POST['name'] . "<br>"; 
    $email = "email :" . $_POST['email'] . "<br>"; 

    $message = " 
\n $subject \n 
\n $message \n 
\n $name \n 
\n $email \n 

"; 
    echo $message; 

    include "PHPMailer_5.2.4/class.phpmailer.php"; 

    $mail = new PHPMailer; 
    $mail->isSMTP(); 
    $mail->Host = 'smtp.gmail.com'; 

    $mail->SMTPAuth = true; 
    $mail->Username = '[email protected]'; 
    $mail->Password = 'password'; 
    $mail->SMTPSecure = 'tls'; 
    $mail->Port  = 587; 
    $mail->setFrom('[email protected]', 'Mailer'); 
    $mail->addAddress('[email protected]', 'Test Name'); 
    $mail->addAttachment('fileaddress'); 
    $mail->isHTML(true); 
    $mail->Subject = $subject; 
    $mail->Body = $message; 
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

    if (!$mail->Send()) { 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } else { 
     echo "Message sent!"; 
    } 
} 

?> 

試試這個工作很好.. !!

+0

謝謝,它工作正常,但電子郵件發件人名稱不會出現在收件箱中,它顯示發件人是「Root用戶」,我試圖編輯這樣的代碼$ name = $ _POST ['name']; $ email = $ _POST ['email'];但id沒有工作,並沒有發送郵件 –

+0

我沒有得到我的本地主機的任何root用戶。 –

相關問題