使用codeigniter从服务器发送邮件

waxmsbnn  于 2023-05-11  发布在  其他
关注(0)|答案(4)|浏览(126)

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

发送邮件的控制器代码

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

在本地主机上使用相同的代码,但在服务器上我得到这个错误。
将协议更改为邮件后,我得到:
Message:mail()[function.mail]:SMTP服务器响应:503此邮件服务器在尝试向非本地电子邮件地址发送邮件时需要身份验证。请检查您的邮件客户端设置或与管理员联系,以验证是否为此服务器定义了域或地址。
文件名:libraries/Email.php
行号:1539
然后我创建了一个新的测试邮件帐户,test@lmsweb.in,并尝试向其发送邮件,它工作了。所以很明显,使用邮件协议,我不能发送邮件到其他像Gmail。

pbwdgjma

pbwdgjma1#

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

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

bvn4nwqk2#

在这一行你没有有效的地址$this->email->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();

希望能帮上忙。

yk9xbfzb

yk9xbfzb3#

$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 ' =>“\r\n”,'newline' =>“\r\n”后,它对我有效

o3imoua4

o3imoua44#

这是对我有效的解决方案:
我们需要使用$_SERVER['REMOTE_ADDR']来获取Controller中的服务器地址:

public function SendEmail()
    {
        $email = $this->input->post('email');

   $message = '
                    <html>
                    <head>
                        <title>Conformation Mail</title>
                    </head>
                    <body>
                        <h1>Hello </h1>
                        <h3>New User</h3>
                        <p>tO cHECK THE EMAIL SENT  </p>
                        <p>Thank Your.</p>
                       
                    </body>
                    </html>';
     //Sending email from localhost or live server
    $localhosts = array(
        '::1',
        '127.0.0.1',
        'localhost'
    );
    
    $protocol = 'mail';
    if (in_array($_SERVER['REMOTE_ADDR'], $localhosts)) {
        $protocol = 'smtp';
    }

    $config = array(
        'protocol' => $protocol,
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '**************@gmail.com',
        'smtp_pass' => '***************',
        'mailtype' => 'html',
        'starttls'  => true,
        'newline'   => "\r\n",
    );

    $this->load->library('email');
    $this->email->initialize($config);
    $this->email->from("************@gmail.com");
    $this->email->to($email);
    $this->email->subject("New user contacts");
    $this->email->message($message);
    $flag = $this->email->send();

    if($flag){
        echo "Email sent";
        echo '<script>alert("email sent")</script>';
        $this->load->view('main');
    }else{
        echo "Email sending failed";
        echo '<script>alert("mail not sent")</script>';
        $this->load->view('main');
    }
}

现在使用控制器功能发送电子邮件。

相关问题