Codeigniter $this->email->send()不工作,而mail()工作

am46iovg  于 2022-12-07  发布在  其他
关注(0)|答案(8)|浏览(161)

我不明白为什么如果我尝试使用CI Email类,它不发送电子邮件,而如果我使用本地PHP mail()类,它可以工作。
必须指出的是,有时我得到“电子邮件发送”,而实际上并没有发送,有时我得到错误“我的服务器没有设置”。
我试着想办法把它装起来但是...什么都没有...
控制器代码如下:

if($this->form_validation->run()){

                //Set Language
                $this->lang->load('site', $this->session->userdata('lang'));

                //Random key
                $user_valid_key = md5(uniqid());

                //Prepare email
                $this->load->library('email', array('mailtype' => 'html'));
                $this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
                $this->email->to($this->input->post('email'));
                $this->email->subject($this->lang->line('email_signup_subject'));

                //Format mail con %s per inserire i campi necessari
                $signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);

                $this->email->message((string)$signup_msg);

                if($this->email->send()){
                    //TODO: load view...
                    echo "email sent";
                }else{
                    $to = $this->input->post('email');
                    mail($to, 'test', 'Other sent option failed');
                    echo $this->input->post('email');
                    show_error($this->email->print_debugger());
                }

                //TODO: Add to db

            }else{

            // Form validation failed

}
b5buobof

b5buobof1#

使用此设置电子邮件..

$this->load->library('email');

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = 'sender_mailid@gmail.com';
$config['smtp_pass']    = 'password';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not      

$this->email->initialize($config);

$this->email->from('sender_mailid@gmail.com', 'sender_name');
$this->email->to('recipient@gmail.com'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();

echo $this->email->print_debugger();
8hhllhi2

8hhllhi22#

我面对这个问题,并找到了以下解决方案。只是在电子邮件配置的一个小变化,它的工作100%:

$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
gt0wga4j

gt0wga4j3#

Codeigniter用户指南:https://www.codeigniter.com/user_guide/libraries/email.html
此设置适合我:

$config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'Your SMTP Server',
            'smtp_port' => 25,
            'smtp_user' => 'Your SMTP User',
            'smtp_pass' => 'Your SMTP Pass',
            'mailtype'  => 'html'
            );
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);

$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address'); 

$this->email->subject('Email Subject');
$this->email->message('Email Message'); 

if($this->email->send()){
   //Success email Sent
   echo $this->email->print_debugger();
}else{
   //Email Failed To Send
   echo $this->email->print_debugger();
}
aiazj4mn

aiazj4mn4#

我用了很多时间在localhost中运行我的配置代码,但它总是给我一个错误(无法使用PHP SMTP发送电子邮件。您的服务器可能没有配置为使用此方法发送邮件。)
但是当在我的服务器中运行下面的代码时,它对我很有效。

应用程序〉控制器〉发送电子邮件_控制器.php

public function send_mail() {
    $this->load->library('email');   
    $config = array();
    $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
    $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
    $config['smtp_user']    = "my@gmail.com"; // client email gmail id
    $config['smtp_pass']    = "******"; // client password
    $config['smtp_port']    =  465;
    $config['smtp_crypto']  = 'ssl';
    $config['smtp_timeout'] = "";
    $config['mailtype']     = "html";
    $config['charset']      = "iso-8859-1";
    $config['newline']      = "\r\n";
    $config['wordwrap']     = TRUE;
    $config['validate']     = FALSE;
    $this->load->library('email', $config); // intializing email library, whitch is defiend in system

    $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break

    $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
    $to_email = $this->input->post('email'); // reciever email, coming from my view page
    //Load email library

    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject('Send Email Codeigniter'); 
    $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
    //Send mail
    if($this->email->send()){
        $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        echo "email_sent";
    }
    else{
        echo "email_not_sent";
        echo $this->email->print_debugger();  // If any error come, its run
    }
}

email视图页面是通过post方法实现的。应用程序〉视图〉emailtesting.php

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
<input type = "email" name = "email"  placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>

如果错误再次出现,请访问以下官方文档:
https://codeigniter.com/user_guide/libraries/email.html

nzkunb0c

nzkunb0c5#

在与同样的问题斗争了几个小时后,我终于决定改变我的配置,通过不同的服务器发送。由于某种原因,我原来的服务器会发送到一些地址,但不是其他地址(在同一个域)。一旦我改变为sendgrid,它就像预期的那样工作。
如果没有得到预期的结果,请尝试其他smtp服务器。问题可能不是您的代码...

py49o6xq

py49o6xq6#

我有同样的问题,我尝试下面的代码,而不是Codeignitor的邮件功能。

mail('mypersonalmail@domainserver.com' , 'Test', 'Test Email');

它的工作和邮件发送到电子邮件地址。该邮件已发送已创建的电子邮件地址(我认为)。在我的情况下,它是:

gtt28651ff@p3plcpnl0552.srod.ahx3.domainserver.com

所以我复制这个电子邮件地址,并尝试与下面的代码。

$this->email->from('gtt28651ff@p3plcpnl0552.srod.ahx3.domainserver.com', 'www.domainserver.com');

而且它工作得很好。似乎有些服务器需要已经创建的电子邮件地址来发送电子邮件,而有些则不需要。
希望这是明确的和有帮助的。

hgtggwj0

hgtggwj07#

最近,通过谷歌SMTP发送电子邮件可能是非常棘手的,由于他们的安全检查。
验证:
1.您的服务器上有rDNS,它与您的服务器IP匹配
1.对您用来发送电子邮件的电子邮件禁用两步验证(在Google设置中)
1.在您用来发送电子邮件的电子邮件上启用IMAP和POP(在Google设置中)
1.在您用来发送电子邮件的电子邮件上允许使用不太安全的应用程序(在Google设置中)
按如下方式更改配置项设置:

//Google settings for CI
    $config['protocol'] = 'ssmtp'; //smtp not working 
    $config['smtp_host'] = 'ssl://ssmtp.gmail.com'; //notice ssmtp
    $config['smtp_user'] = 'anyemail@server.com'; //must use Google
    $config['smtp_pass'] = '***********';
    $config['smtp_port'] = '465';
    $config['smtp_crypto'] = 'ssl';

    //general settings
    $config['_smtp_auth'] = TRUE; //important
    $config['smtp_timeout'] = 30;
    $config['charset'] = 'utf-8';
    $config['mailtype'] = 'html';
    //optional
    $config['wrapchars'] = 76;
    $config['wordwrap'] = TRUE;

我希望这将保存你的2天,我一直在这个问题上挣扎。

ocebsuys

ocebsuys8#

我已经花了几个小时试图改变端口,在php.ini文件中启用ssl扩展等没有任何工作,只结束了一个消息SMTP服务器没有正确配置.我使用WAMP作为本地主机,当我关闭我的防病毒avast相同的配置工作!!!发送successfully.so它可能是您的防病毒程序阻止在本地机器上.

相关问题