0
votes

I have application where I want to send mails to clients but i am getting error from server.

Before Scrollenter image description here

Controller code for sending mail

        $this->load->library('email');
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'mail.lmsweb.in';
        $config['smtp_port'] = '25';
        $config['smtp_user'] = '[email protected]';
        $config['smtp_pass'] = '****';
    //  $config['charset'] = 'iso-8859-1';

        $this->email->initialize($config);
        $this->email->from('support@lmsweb', 'LMS');
        $this->email->to($this->input->post('txtvisitoremail'));
        $this->email->subject('Client Login Details');
        $this->email->subject('Congratulation');
        $this->email->send();
       
        echo $this->email->print_debugger();
        redirect('telecaller/telecaller/update');

Same code works when using on localhost but on server I get this error.

After changing protocol to mail I get:

Message: mail() [function.mail]: SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

Filename: libraries/Email.php

Line Number: 1539

I then created a new test mail account, [email protected] and tried sending mail to it and it worked. So its clear that using mail protocol I cannot send mail to other like Gmail.

3
Does adding $config['newline'] = "\r\n"; and $config['charset'] = 'UTF-8'; help at all? - Daniel Waghorn
@DanielWaghorn no i tried all those settings even tried changing smtp to other protocol - ALOK

3 Answers

0
votes

I used this code to send mail from server:

function sendEmail($data, $templateName) {
    //echo "<pre>";

    //print_r($data);

    $this->load->library('email');
    $this->email->set_mailtype('html');
    $this->email->from($data['from_address']);
    $this->email->to($data['to_address']);
    //$this->email->cc($data['cc_address']);
    $this->email->subject($data['subject']);
    $body = '--------'

    $this->email->message($body);
    $this->email->send();
    $this->email->clear();
    //redirect('web_welcome/member_signup');
}
0
votes

In this line you don't have valid address $this->email->from('support@lmsweb', 'LMS'); You are lacking .in: support@lmsweb .in

Anyway you can use the PHP Mailer class for sending emails from codeigniter. I've done it by putting the files class.phpmailer.php and class.smtp.php in third party folder and then creating custom library for mail sending:

class Mail_sender
{


protected $ci;

var $mail;

public function __construct()
{   
    include (APPPATH. 'third_party/class.phpmailer.php');
    $this->ci =& get_instance();
    $this->mail = new PHPMailer ();
    $this->init();

}
public function init(){
    $this->mail->IsSMTP ();
    $this->mail->IsHTML(true);
    $this->mail->Mailer = 'smtp';
    $this->mail->SMTPAuth = false;
    $this->mail->Host = 'smtp.t-home.mk'; // "ssl://smtp.gmail.com" didn't worked
    $this->mail->Port = 25;
    $this->mail->SMTPSecure = '';
    $this->mail->SMTPAuth = true;  
    $this->mail->Username = '[email protected]';       // SMTP username
    $this->mail->Password = 'secret';  //SMTP password
    $this->mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.       


}
public function setUsername($username){
    $this->mail->Username = $username;
}

public function setFrom($email, $sender_name)   {

    $this->mail->From = $email;
    $this->mail->FromName = $sender_name;
}

public function setAddress($to){
    $this->mail->addAddress ($to);
}

public function setReplyTo($replyTo)
{
    $this->mail->AddReplyTo($replyTo);
}

public function setSubject($subject){
    $this->mail->Subject = $subject;
    return $this->mail->Subject;
}

public function setBody($messageBody){
    $this->mail->Body = $messageBody;
}

public function sendMail(){
    return $this->mail->Send();
}

}

And used a controller for sending mail:

$nameSurname=$this->input->post('name');
    $email = $this->input->post('email');
    $subject = $this->input->post('subject');
    $temp_message = $this->input->post('message');
    $message = 'FROM: '.$nameSurname.'('.$email.')<br><br>'.$temp_message;
    $array = array(
        'ime' => $nameSurname,
        'mail' => $email,
        'sub' => $subject,
        'msg' => $message
        );

    $this->load->library('mail_sender');
    $this->mail_sender->setFrom('[from address]','[title]');
    $this->mail_sender->setSubject($subject);
    $this->mail_sender->setAddress("[receiving address]");

    $this->mail_sender->setBody($message);

    $this->mail_sender->sendMail();

Hope it helps.

0
votes
$config = Array(
 'protocol' => 'smtp',
 'smtp_host' => 'ssl://smtp.googlemail.com',
 'smtp_port' => 465,
 'smtp_user' => '[email protected]', // change it to yours
 'smtp_pass' => '*******', // change it to yours
 'wordwrap' => TRUE,
  'crlf' => "\r\n",
  'newline' => "\r\n" );

it is worked for me after adding last two lines ''crlf' => "\r\n", 'newline' => "\r\n"'