2016-08-07 56 views
0

我有這個簡單的PHP郵件功能PHP簡單的郵件功能無法正常工作

$to = "[email protected]"; 
$subject = "My subject"; 
$txt = "Hello world!"; 
$headers = "From: [email protected]" . "\r\n" . 
"CC: [email protected]"; 
if(mail($to,$subject,$txt,$headers)){ 
    echo "done"; 
} 

它回聲完成,但是當我檢查我的電子郵件有垃圾郵件部分

+0

這是在本地主機或託管的服務器上? – Qirel

+0

Localhost @Qirel –

+0

郵件功能在localhost中不起作用 –

回答

0

使用PHPMailer的庫nothing.Even。這比使用郵件本機功能要好,因爲在PHPMailer中,您可以使用用戶身份驗證來避免將電子郵件發送給垃圾郵件。您可以在不配置郵件服務器的情況下使用此庫。它更容易調試。您可以在這個環節https://github.com/PHPMailer/PHPMailer

下載,見一個例子:

$mail    = new PHPMailer(); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "mail.yourdomain.com"; // SMTP server 
$mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
              // 1 = errors and messages 
              // 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "tls";     
$mail->Host  = "smtp.gmail.com";  // SMTP server 
$mail->Port  = 587;     // SMTP port 
$mail->Username = "[email protected]"; // username 
$mail->Password = "yourpassword";   // password 

$mail->SetFrom('[email protected]', 'Test'); 

$mail->Subject = "I hope this works!"; 

$mail->MsgHTML('Blah'); 

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

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

我已經嘗試過使用this.but它只需要太長的時間來加載 –

+0

這可能是您的本地連接與服務器主機的速度.PHPMailer是在大多數應用程序中使用的PHP郵件庫之一。 – msantos

+0

使用PHPMailer你不需要配置本地郵件服務器; – msantos