2012-08-23 92 views
2

我有一個形式,用戶將能夠發送他們的郵件到不同的郵件ID。該表單具有諸如from,subject,subject等字段,用戶將在'from'字段中給他們的郵件ID,在'to'字段中給出接收者的郵件ID。問題是我不能發送郵件,但我認爲我的代碼沒問題。 CodeIgniter是否允許以這種方式發送電子郵件?發送郵件與codeigniter

控制器

class Email_send extends CI_Controller { 

    function Email_send() 
    { 
     parent::__construct(); 
     $this->load->library('email'); 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
    } 

    function index() 
    { 
     $config = Array(
      'protocol' => 'smtp', 
      'smtp_host' => 'ssl://smtp.googlemail.com', 
      'smtp_port' => 465, 
      'subject' => $this->input->post('subject'), 
      'message' => $this->input->post('message'), 
      'from' => $this->input->post('from'), 
      'to' => $this->input->post('to'), 
     ); 

     $this->load->library('email', $config); 
     $this->email->set_newline("\r\n"); 

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

     $this->email->from($config['subject']); 
     $this->email->to($config['to']); 

     $this->email->subject($config['from']); 
     $this->email->message($config['message']); 

     if($this->email->send()) 
     { 
      $this->load->view('success_em'); 
     } 

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

查看

 echo form_open('Email_send', $attributes); ?> 

<p> 
     <label for="from">Your mail ID <span class="required">*</span></label> 
     <?php echo form_error('from'); ?> 
     <br /><input id="name" type="text" name="from"value="<?php echo set_value('from'); ?>" /> 
</p> 

<p> 
     <label for="to">To <span class="required">*</span></label> 
     <?php echo form_error('to'); ?> 
     <br /><input id="to" type="text" name="to" value="<?php echo set_value('to'); ?>" /> 
</p> 

<p> 
     <label for="subject">Subject of the mail<span class="required">*</span></label> 
     <?php echo form_error('subject'); ?> 
     <br /><input id="subject" type="text" name="subject" value="<?php echo set_value('subject'); ?>" /> 
</p> 


<p> 
     <label for="message">Your Message<span class="required">*</span></label> 
     <?php echo form_error('message'); ?> 
     <br /><textarea id="message" style="width:300px; height:80px" type="text" name="message" value="<?php echo set_value('message'); ?>" /></textarea> 

錯誤

The following SMTP error was encountered: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 ko8sm6157345pbc.40 
502 5.5.1 Unrecognized command. ko8sm6157345pbc.40 
The following SMTP error was encountered: 502 5.5.1 Unrecognized command. ko8sm6157345pbc.40 
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method. 

User-Agent: CodeIgniter 
Date: Thu, 23 Aug 2012 14:54:54 +0000 
From: 
Return-Path: 
To: [email protected] 
Subject: [email protected]?= 
Reply-To: "hello" 
X-Sender: hello 
X-Mailer: CodeIgniter 
X-Priority: 3 (Normal) 
Message-ID: <5036443e415e4> 
Mime-Version: 1.0 


Content-Type: text/plain; charset=utf-8 
Content-Transfer-Encoding: 8bit 

hello 
+0

您是否檢查過日誌?你遇到了什麼錯誤? – jco

+1

我認爲你從代碼中看到了「成功」的消息,否則你會發布它從$ this-> email-> print_debugger()打印的消息... – sourcejedi

回答

1

您需要進行身份驗證冷杉噸。您錯過了這兩個參數:

$config['smtp_user'] = "[email protected]"; 
$config['smtp_pass'] = 'yourpassword'; 
+0

是的,我試圖從本地發送服務器到Hotmail或Gmail。響應是添加PLZ檢查。謝謝。 – martan

+0

你知道是否有可能以這種方式發送郵件到雅虎或Hotmail?謝謝 – martan

+1

您可以發送到任何電子郵件,它是雅虎或Hotmail。按照說明對Gmail進行身份驗證。只需將目標電子郵件地址設置爲$ this-> email->即可(「[email protected]」); – Hatem

0

您需要先認證。您錯過了這兩個參數:

$config['smtp_user'] = "[email protected]"; 
$config['smtp_pass'] = 'yourpassword'; 

將它們添加到其他配置旁邊。

1

我不認爲你需要添加subjectmessagefromtoconfig反對你的傳球給init電子郵件類。

嘗試刪除這些幷包含SMTP認證密鑰,如Hatem建議。

E.g.

$config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.googlemail.com', 
    'smtp_port' => 465, 
    'smtp_user' => '[email protected]'; 
    'smtp_pass' => 'yourpassword'; 
);