从服务器使用codeigniter发送邮件

时间:2015-07-04 17:16:36

标签: php codeigniter email gmail

我有应用程序,我想向客户发送邮件,但我从服务器收到错误。

Before Scroll enter image description here

发送邮件的控制器代码

        $this->load->library('email');
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'mail.lmsweb.in';
        $config['smtp_port'] = '25';
        $config['smtp_user'] = 'support@lmsweb.in';
        $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');

在localhost上使用相同的代码,但在服务器上我得到此错误。

将协议更改为邮件后,我得到:

  

消息:mail()[function.mail]:SMTP服务器响应:503此邮件服务器在尝试发送到非本地电子邮件地址时需要进行身份验证。请检查您的邮件客户端设置或联系您的管理员以验证是否为此服务器定义了域或地址。

     

文件名:libraries / Email.php

     

行号:1539

然后我创建了一个新的测试邮件帐户test@lmsweb.in并尝试向其发送邮件并且它有效。所以很明显,使用邮件协议我无法向Gmail之类的其他邮件发送邮件。

3 个答案:

答案 0 :(得分:0)

我使用此代码从服务器发送邮件:

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

答案 1 :(得分:0)

在此行中,您没有有效地址$ this-&gt; email-&gt; from('support @ lmsweb','LMS');您缺少 .in :support @ lmsweb .in

无论如何,您可以使用PHP Mailer课程从codeigniter发送电子邮件。我通过将文件class.phpmailer.php和class.smtp.php放在第三方文件夹中然后创建用于邮件发送的自定义库来完成它:

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 = 'user@example.com';       // 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();
}

}

并使用控制器发送邮件:

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

希望它有所帮助。

答案 2 :(得分:0)

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

在添加最后两行后,它对我有用''crlf'=&gt; “\ r \ n”,   'newline'=&gt;为 “\ r \ n” 个“