2016-05-16 24 views
0

首先,我是新來的和PHP我如何添加smtp代碼

我的問題是關於Smtp支持。我的主機需要smtp支持。我沒有添加它。我讀了很多帖子,但我的知識基本。我怎樣才能在這個文件中添加的smtp代碼,請幫我

<?php 
    //start a session -- needed for Securimage Captcha check 
    session_start(); 

    //add you e-mail address here 
    define("MY_EMAIL", "[email protected],[email protected]"); 

    /** 
    * Sets error header and json error message response. 
    * 
    * @param String $messsage error message of response 
    * @return void 
    */ 
    function errorResponse ($messsage) { 
    header('HTTP/1.1 500 Internal Server Error'); 
    die(json_encode(array('message' => $messsage))); 
    } 

    /** 
    * Return a formatted message body of the form: 
    * Name: <name of submitter> 
    * Comment: <message/comment submitted by user> 
    * 
    * @param String $name  name of submitter 
    * @param String $messsage message/comment submitted 
    */ 
    function setMessageBody ($name, $phone, $email, $message) { 
    $message_body .= "Name: " . $name."\n\n"; 
    $message_body .= "Phone: " . $phone."\n\n"; 
    $message_body .= "Email: " . $email."\n\n"; 
    $message_body .= "Message:" . $message."\n\n"; 
    return $message_body; 
    } 

$name = $_POST['name']; 
$phone = $_POST['phone']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

    header('Content-type: application/json'); 
    //do some simple validation. this should have been validated on the client-side also 
    if (empty($email) || empty($message)) { 
    errorResponse('Email or message is empty.'); 
    } 

    //do Captcha check, make sure the submitter is not a robot:)... 
    include_once './vender/securimage/securimage.php'; 
    $securimage = new Securimage(); 
    if (!$securimage->check($_POST['captcha_code'])) { 
    errorResponse('Invalid Security Code'); 
    } 
//try to send the message 
if(mail(MY_EMAIL, "Feedback Form Results", setMessageBody($_POST["name"], $_POST["phone"], $_POST["email"], $_POST["message"]), "From:$email")) { 
echo json_encode(array('message' => 'Your message was successfully submitted.')); 

} else { 
header('HTTP/1.1 500 Internal Server Error'); 
echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.')); 
} 
?> 
+0

你是什麼意思加入smtp代碼(和在哪裏)?你的腳本的當前輸出是什麼以及需要的是什麼? – Buksy

+0

使用一些類似SwiftMailer的庫來使用SMTP發送電子郵件。還要確保你的PHP有SMTP支持(查看phpinfo(),如果缺失,必須重新編譯PHP)。 – Justinas

+0

您可以在https://github.com/PHPMailer/PHPMailer上嘗試解決方案。這是解決方案有完整的文檔來開發。 –

回答

0
$host = "mailserver.blahblah.com"; 
$username = "smtp_username"; 
$password = "smtp_password"; 

$smtp = Mail::factory('smtp', array ('host' => $host, 
            'auth' => true, 
            'username' => $username, 
            'password' => $password)); 

$mail = $smtp->send($to, $headers, $body);