发送带有codeigniter的邮件

时间:2012-08-23 14:39:05

标签: codeigniter

我有一个表单,用户可以将邮件发送到不同的邮件ID。表单包含from,to,subject等字段。用户将在“from”字段中提供邮件ID,在“to”字段中提供接收者的邮件ID。问题是我无法发送邮件,但我觉得我的代码还可以。 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: xxxxx@gmail.com
Subject: =?utf-8?Q?xxxxx@hotmail.com?=
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

3 个答案:

答案 0 :(得分:1)

您需要先进行身份验证。你错过了这两个参数:

$config['smtp_user'] = "user@gmail.com";
$config['smtp_pass'] = 'yourpassword';

答案 1 :(得分:1)

我认为您不需要在传递的subject对象中添加messagefromtoconfig来初始化电子邮件类

尝试删除这些内容并将其包含在Hatem建议的SMTP身份验证密钥中。

<强> E.g。

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'user@gmail.com';
    'smtp_pass' => 'yourpassword';  
);

答案 2 :(得分:0)

您需要先进行身份验证。你错过了这两个参数:

$config['smtp_user'] = "user@gmail.com";
$config['smtp_pass'] = 'yourpassword';

将它们添加到其他配置旁边。