2013-06-24 110 views
14

我想在CodeIgniter上設置SMTP。一切工作正常,我收到頁面上的成功消息,該電子郵件發送沒有錯誤。但是,電子郵件未送達。CodeIgniter上的SMTP顯示成功,但電子郵件未發送到Gmail帳戶

下面是代碼,我用:

$config = Array(
'protocol' => 'smtp', 
'smtp_host' => 'ssl://smtp.googlemail.com', 
'smtp_port' => 465, 
'smtp_user' => '[email protected]', 
'smtp_pass' => '***', 
'mailtype' => 'html', 
'charset' => 'iso-8859-1', 
'wordwrap' => TRUE 
); 
$this->load->library('email', $config); 

$this->email->from('[email protected]', 'Explendid Videos'); 
$this->email->to('[email protected]'); 
$this->email->reply_to('[email protected]', 'Explendid Videos'); 


$this->email->subject('Explendid Video - Contact form'); 

$message = "Contact form\n\n"; 
$message .= "Name: ". $_POST['name'] . "\n"; 
$message .= "Phone: ". $_POST['phone'] . "\n"; 
$message .= "Email: ". $_POST['email'] . "\n"; 

$this->email->message($message); 

$this->email->send(); 

什麼可以的原因,該電子郵件沒有實際交付。

回答

30

將其更改爲以下:

$ci = get_instance(); 
$ci->load->library('email'); 
$config['protocol'] = "smtp"; 
$config['smtp_host'] = "ssl://smtp.gmail.com"; 
$config['smtp_port'] = "465"; 
$config['smtp_user'] = "[email protected]"; 
$config['smtp_pass'] = "yourpassword"; 
$config['charset'] = "utf-8"; 
$config['mailtype'] = "html"; 
$config['newline'] = "\r\n"; 

$ci->email->initialize($config); 

$ci->email->from('[email protected]', 'Blabla'); 
$list = array('[email protected]'); 
$ci->email->to($list); 
$this->email->reply_to('[email protected]', 'Explendid Videos'); 
$ci->email->subject('This is an email test'); 
$ci->email->message('It is working. Great!'); 
$ci->email->send(); 
+3

感謝它的工作,通過改變「SSL://smtp.googlemail.com 「to」ssl://smtp.gmail.com「 –

+2

這給了我'fsockopen():php_network_getaddresses:getaddrinfo失敗:名稱或服務未知'。從工作地址開頭刪除'ssl://' – machineaddict

+0

將它適用於hotmail/outlook以及 - – lazyme114

0

你檢查了你的php.ini文件嗎?嘗試一下。如果沒有,那麼也許你也可以嘗試SPF。 SPF或Sender Policy Framework是一項新技術,可以輕鬆檢測到垃圾郵件。除非您手動將這些電子郵件標記爲非垃圾郵件,否則Gmail會尊重SPF。無論如何,如果您收到了另一個地址的電子郵件,那麼他們也必須到達Gmail。徹底檢查您的垃圾郵件,因爲Gmail即使在非常高的垃圾郵件嫌疑的情況下也不放棄電子郵件,而是最終進入垃圾郵件文件夾。

您可以設置一個SPF,讓你的服務器來發送電子郵件,這將導致在Gmail接受您的網絡服務器發送正宗電子郵件。請參閱http://www.mydigitallife.info/how-to-set-up-and-create-sender-policy-framework-spf-domain-dns-txt-record-with-wizard/和Microsoft的嚮導。

+0

電子郵件不是在垃圾郵件文件夾會 –

0

你可以改變這個腳本,用於調試您的問題,

$this->email->send(); 

if($this->email->send()) 
{ 
    echo 'Your email was sent.'; 
} 

else 
{ 
    show_error($this->email->print_debugger()); 
} 
4

這裏是爲我工作的的Apache2服務器,ci 2.1.4:其非常簡單: 首先在你的application/co下創建一個名爲email.php的文件nfig目錄,然後輸入以下代碼在其內部〜>

<?php 
$config['protocol'] = 'smtp'; 
$config['smtp_host'] = 'ssl://smtp.gmail.com'; 
$config['smtp_port'] = '465'; 
$config['smtp_user'] = 'u'r gmail account'; 
$config['smtp_pass'] = 'password of u'r account'; 
$config['charset'] = 'utf-8'; 
$config['newline'] = "\r\n"; 
?> 

然後創建一個應用程序/控制器目錄下名爲email.php然後輸入這個代碼文件〜>

<?php 
    class Email extends CI_Controller 
    { 

    function send() 
    { 
    // Loads the email library 
    $this->load->library('email'); 
    // FCPATH refers to the CodeIgniter install directory 
    // Specifying a file to be attached with the email 
    // if u wish attach a file uncomment the script bellow: 
    //$file = FCPATH . 'yourfilename.txt'; 
    // Defines the email details 
    $this->email->from('[email protected]', 'ur Name'); 
    $this->email->to('[email protected]'); 
    $this->email->subject('Email Test'); 
    $this->email->message('Testing the email class.'); 
    //also this script 
    //$this->email->attach($file); 
    // The email->send() statement will return a true or false 
    // If true, the email will be sent 
    if ($this->email->send()) { 
    echo "you are luck!"; 
    } else { 
    echo $this->email->print_debugger(); 
    } 
    } 

    } 
    ?> 
2

更換

$config['protocol'] = 'smtp'; 

$config['protocol'] = 'sendmail'; 
+0

這對我來說簡直就是訣竅,簡單卻很棒 - 謝謝! – Raymond

+0

請爲您的答案添加一些解釋,以使其對其他讀者更有用 –

+0

It Worked。嘗試過許多其他解決方案好幾個小時,非常感謝 –

0

使用下面的代碼

而且froget不要在谷歌無法以下兩個安全設置。

1)https://www.google.com/settings/security/lesssecureapps < <打開它

2)https://accounts.google.com/b/0/DisplayUnlockCaptcha < <點擊繼續

**關閉兩步驟驗證,如果您有它啓用。

$config = Array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.gmail.com', 
     'smtp_port' => 465, 
     'smtp_user' => '[email protected]', //email id 
     'smtp_pass' => 'xxxxxxxxxxx',   // password 
     'mailtype' => 'html', 
     'charset' => 'iso-8859-1' 
    ); 
    $this->load->library('email', $config); 
    $this->email->set_newline("\r\n"); 

    $this->email->from('[email protected]','my name'); 
    $this->email->to("[email protected]"); // email array 
    $this->email->subject('email subject'); 
    $this->email->message("my mail body"); 

    $result = $this->email->send(); 


    show_error($this->email->print_debugger()); // for debugging purpose :: remove this once it works... 
0

我只是修改了代碼從RobinCominotto,使其在office365工作。PS:我把它放在一個控制器中,然後像這樣調用這個函數。 !當我把這個CONFIGS上email.php(配置文件)不工作:(

$ci = get_instance(); 
    $ci->load->library('email'); 
    $config['protocol'] = "smtp"; 
    $config['smtp_host'] = "smtp.office365.com"; 
    $config['smtp_port'] = "587"; 
    $config['smtp_user'] = "<HERE COMES YOUR EMAIL>"; 
    $config['smtp_pass'] = "<HERE COMES THE PASSWORD OF EMAIL>"; 
    $config['charset'] = "utf-8"; 
    $config['mailtype'] = "html"; 
    $config['newline'] = "\r\n"; 

    $ci->email->initialize($config); 

    $ci->email->from('<HERE COMES YOUR EMAIL>', 'Blabla'); 
    $list = array('<HERE COMES TO EMAIL>', '<HERE COMES TO EMAIL>'); 
    $ci->email->to($list); 
    $this->email->reply_to('<HERE COMES YOUR EMAIL>', 'Explendid Videos'); 
    $ci->email->subject('This is an email test'); 
    $ci->email->message('It is working. Great!'); 
    $ci->email->send(); 
    print_r($ci->email->print_debugger()); 
相關問題