2015-08-09 101 views
0

我想通過PHP發送郵件.. 我已經嘗試通過php的郵件功能和phpmailer()函數了。 但我無法發送它 我已經嘗試通過更改php.ini中的設置tooby設置端口號。至465,25 和獲取幫助過網,但仍然是我的郵件不能正常工作多一些設置,我的代碼身份驗證錯誤儘管發送郵件在php

<html> 
<head> 
<title>PHPMailer - SMTP (Gmail) basic test</title> 
</head> 
<body> 

<?php 
date_default_timezone_set('asia/calcutta'); 

require_once('class.phpmailer.php'); 
$mail    = new PHPMailer(); 

$body    = "testing message"; 

$mail->IsSMTP(); // telling the class to use SMTP 

$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 465;     // set the SMTP port for the GMAIL server 
$mail->Username = $_POST["u"]; // GMAIL username 
$mail->Password = $_POST["p"];   // GMAIL password 

$mail->SetFrom($_POST["u"], 'First Last'); 

$mail->Subject = "hello"; 

    $mail->MsgHTML($body); 

$address = $_POST["to"]; 
$mail->AddAddress($address, "info"); 

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

?> 

</body> 
</html> 

我有一個其他頁面以用戶名,密碼,發件人的電子郵件,並獲得DEM DIS頁。而我得到的錯誤是這樣的:

Mailer Error: The following From address failed: s********@g***l.com : MAIL not accepted from server,530,5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 sa9sm15580073pbc.18 - gsmtp

SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 https:/support.google.com/mail/answer/14257 sa9sm15580073pbc.18 - gsmtp

SMTP server error: 5.5.1 Authentication Required. Learn more at 530 5.5.1 https:/support.google.com/mail/answer/14257 sa9sm15580073pbc.18 - gsmtp

有時我也得到一個錯誤信息說:

called mail() without being connected mailer error in php

請幫助我的人.... 並提前致謝

+0

你根據你的一個老示例代碼,並可能正在使用舊版本的PHPMailer的。 [獲取最新版本](https://github.com/PHPMailer/PHPMailer)。之後,[請閱讀故障排除文檔](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting)。 – Synchro

+1

嘗試'$ mail-> SMTPSecure ='tls';',仔細檢查用戶名和密碼。您可以檢查PHPMailer [gmail文檔](https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps)。 –

回答

0

這是我的phpMailer。 希望它會幫助你

require RB_ROOT.'/PHPMailer-master/PHPMailerAutoload.php'; 

define('GLAVNIMAIL', '[email protected]'); 
define('PASSMAIL', 'xxxxxxxxx'); // enable 2 way notification on gmail to get this code 

$mail = new PHPMailer; 
//$mail->SMTPDebug = 4; 
$mail->CharSet = 'UTF-8'; 
$mail->isSMTP(); 
$mail->Debugoutput = 'html'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 587; 
$mail->SMTPSecure = 'tls'; 
$mail->SMTPAuth = true; 
$mail->Username = GLAVNIMAIL; 
$mail->Password = PASSMAIL; 
$mail->From = GLAVNIMAIL; 
$mail->FromName = 'Title From'; 
$mail->isHTML(true); 
$mail->addAddress($email, 'Nov Korisnik');  // Add a recipient 
//$mail->addReplyTo($email, $korpaime.' '.$korpaprezime); 
//$mail->addCC('[email protected]'); 
$mail->addBCC(GLAVNIMAIL); 
//$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 

$mail->Subject = 'Registracija korisnika '.$email; 
$mail->Body = $bodyMail; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

if (!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    die; 
} else { 
    echo 'OK poslat mail'; 

這是鏈接PHP MAILER

希望它可以幫助