martan
martan

Reputation: 95

sending mails with codeigniter

i have a form where the users will be able to send their messages to different mail ids. The form has fields like from, to, subject etc. the users will give their mail id in the 'from' filed and the receiver's mail id in the 'to' fields. the problem is i'm not being able to send mail though i think my code is ok. Does CodeIgniter allow to send emails in this way??

Controller

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());
        }
    }
}

View

        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>

Error

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

Upvotes: 2

Views: 1383

Answers (3)

Malachi
Malachi

Reputation: 33720

I don't think you need to add subject, message, from, to in the config object your passing to init the email class.

Try removing these and include the SMTP authentication keys as Hatem suggested.

E.g.

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

Upvotes: 1

Hatem
Hatem

Reputation: 589

You need to authenticate first. You miss these two parameters:

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

Add them beside other configurations.

Upvotes: 0

Hatem
Hatem

Reputation: 589

You need to authenticate first. You miss these two parameters:

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

Upvotes: 1

Related Questions