2012-08-23 94 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'; 
);