2016-03-08 62 views
0

我有幾個字段和形式的細節一個簡單的PHP形式使用下述下面的代碼發送到發送電子郵件..無法使用PHP

甚至認爲我拿到「謝謝你,你的訊息話題已收到。」消息,但消息永遠不會通過。

我試圖改變$use_smtp = '0';仍然無法正常工作

<?php 
include('SMTPClass.php'); 

$use_smtp = '1'; 
$emailto = '[email protected]'; 

    // retrieve from parameters 
    $emailfrom = isset($_POST["email"]) ? $_POST["email"] : ""; 
    $nocomment = isset($_POST["nocomment"]) ? $_POST["nocomment"] : ""; 
    $subject = 'Email from Website'; 
    $message = ''; 
    $response = 'Thank you for your message. I will be in contact shortly.'; 
    $response_fail = 'There was an error verifying your details.'; 

     // Honeypot captcha 
     if($nocomment == "") { 

      $params = $_POST; 
      foreach ($params as $key=>$value){ 

       if(!($key == 'ip' || $key == 'emailsubject' || $key == 'url' || $key == 'emailto' || $key == 'nocomment' || $key == 'v_error' || $key == 'v_email')){ 

        $key = ucwords(str_replace("-", " ", $key)); 

        if (gettype($value) == "array"){ 
         $message .= "$key: \n"; 
         foreach ($value as $two_dim_value) 
         $message .= "...$two_dim_value<br>"; 
        }else { 
         $message .= $value != '' ? "$key: $value\n" : ''; 
        } 
       } 
      } 

     $response = sendEmail($subject, $message, $emailto, $emailfrom); 

     } else { 

      $response = $response_fail; 

     } 

    echo $response; 

// Run server-side validation 
function sendEmail($subject, $content, $emailto, $emailfrom) { 

    $from = $emailfrom; 
    $response_sent = 'Thank you. Your messsage has been received.'; 
    $response_error = 'Error. Please try again.'; 
    $subject = filter($subject); 
    $url = "Origin Page: ".$_SERVER['HTTP_REFERER']; 
    $ip = "IP Address: ".$_SERVER["REMOTE_ADDR"]; 
    $message = $content."\n$ip\r\n$url"; 

    // Validate return email & inform admin 
    $emailto = filter($emailto); 

    // Setup final message 
    $body = wordwrap($message); 

    if($use_smtp == '1'){ 

     $SmtpServer = 'smtp.office365.com'; 
     $SmtpPort = '587'; 
     $SmtpUser = '[email protected]'; 
     $SmtpPass = 'password'; 

     $to = $emailto; 
     $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body); 
     $SMTPChat = $SMTPMail->SendMail(); 
     $response = $SMTPChat ? $response_sent : $response_error; 

    } else { 

     // Create header 
     $headers = "From: $from\r\n"; 
     $headers .= "MIME-Version: 1.0\r\n"; 
     $headers .= "Content-type: text/plain; charset=utf-8\r\n"; 
     $headers .= "Content-Transfer-Encoding: quoted-printable\r\n"; 

     // Send email 
     $mail_sent = @mail($emailto, $subject, $body, $headers); 
     $response = $mail_sent ? $response_sent : $response_error; 

    } 
    return $response; 
} 

// Remove any un-safe values to prevent email injection 
function filter($value) { 
    $pattern = array("/\n/", "/\r/", "/content-type:/i", "/to:/i", "/from:/i", "/cc:/i"); 
    $value = preg_replace($pattern, "", $value); 
    return $value; 
} 

exit; 

?> 
+0

一郵件的真實返回值意味着郵件服務器收到並接受了郵件。這並不意味着郵件已發送或目標郵件服務器已接受它。這就是你可以檢查的。您可能需要檢查郵件服務器。 –

+0

這是不正確的方式發送電子郵件,因爲我使用憑據進行身份驗證..或者我應該採取不同的方法 – Learning

+0

如果您使用SMTP,您需要使用信用卡進行身份驗證。 –

回答