2016-09-27 91 views
2

我需要花費幾天時間才能獲得正確的設置,所以我想我會發佈一個適用於Bluehost的php腳本。在初始測試中使用isSMTP比isMAIL更快。如何在Bluehost上使用phpMailer isSMTP?

<?php 
require_once '../includes/phpmailer/PHPMailerAutoload.php'; 

$mail = new PHPMailer(); 

$mail->IsSMTP();       // set mailer to use SMTP 
$mail->Host = "box1311.bluehost.com"; // specify bluehost as outgoing server 
$mail->SMTPSecure = "tls";    // sets the prefix to the server do not use ssl 
$mail->SMTPDebug = 3;     // comment out if you don't need debug info 
$mail->SMTPAuth = true;    // turn on SMTP authentication 
$mail->Username = "[email protected]"; // SMTP username (your email account) 
$mail->Password = "PASSWORD";   // SMTP password 
$mail->Port  = 25; 
$mail->From  = '[email protected]'; 
$mail->FromName = "[email protected]"; 
$mail->AddAddress('[email protected]'); 
$mail->IsHTML(true);      // set email format to HTML 

$mail->Subject = 'test message'; 
$body = '<!DOCTYPE html> 
      <html><header> 
       </header> 
       <body lang=EN-US> 
        <div style="text-align:center"> 
         <h2>this is a test</h2> 
        </div> 
       </body> 
      </html>'; 
$mail->Body = $body; 

$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; 

if(!$mail->Send()){ 
    echo "Message could not be sent. <p>"; 
    echo "Mailer Error: " . $mail->ErrorInfo; 
}else{ 
    echo '<h1>message sent</h1>'; 
} 
?> 
+0

如果這是一個解決方案,你應該發佈一個(可搜索的)問題,然後張貼你的貢獻作爲一個實際的答案。 [你可以回答你自己的問題](http://meta.stackexchange.com/q/17463/300177),並會看到一個複選框,說***你想回答你自己的問題嗎?***(或者這個效果的東西)。 –

+0

非常感謝你!幫助了我很多! –

回答

0

此代碼適用於我。

include "phpmailer/class.phpmailer.php"; 
include "phpmailer/class.smtp.php"; 

$email_user = "[email protected]"; 
$email_password = "pass123"; 
$the_subject = "Title"; 
$from_name = "Sender"; 
$phpmailer = new PHPMailer(); 

// ---------- datos de la cuenta de correo ----------------------------- 
$phpmailer->Username = $email_user; 
$phpmailer->Password = $email_password; 
//--------------------------------------------------------------------- 
$phpmailer->SMTPSecure = 'tls'; 
$phpmailer->Host = "box6171.bluehost.com"; 
$phpmailer->Port = 26; 
//$phpmailer->SMTPDebug = 2; 
$phpmailer->IsSMTP(); 
$phpmailer->SMTPAuth = true; 

$phpmailer->setFrom($phpmailer->Username,$from_name); 
$phpmailer->AddAddress("[email protected]"); 
$phpmailer->Subject = $the_subject; 

$phpmailer->Body .="<h1 style='color:#3498db;'>Attachment:</h1>"; 
$phpmailer->Body .= "<h3>".$attach1."</h3>"; 

$phpmailer->AddAttachment($attach, "attach1"); 
$phpmailer->AddBCC("[email protected]", "bcc1"); 
$phpmailer->IsHTML(true); 
$enviado = $phpmailer->Send(); 
if($enviado) { 
    echo 'email send successful'; 
}