在CodeIgniter中发送电子邮件

时间:2014-05-24 16:37:16

标签: php codeigniter

我是CodeIgniter的新手。我是否知道如何通过localhost在CodeIgniter中发送电子邮件?我应该在smtp_usersmtp_pass中添加什么内容?以下是我的控制器代码:

function Forgot_Password()
    {
        $this->form_validation->set_rules('Email', 'Email', 'trim|required|valid_email|callback_email_check');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('Forgot_Password');
            $this->load->view('templates/footer'); 
        }
        else
        {
            $this->load->library('email');


           $config = Array(
               'protocol' => 'smtp',
               'smtp_host' => 'ssl://smtp.googlemail.com',
               'smtp_port' => 465,
               'smtp_user' => 'xxxxxxxxxx',
               'smtp_pass' => 'xxxxxxxxxx'
               );

            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->from('26328@siswa.unimas.my', 'Your Name');
            $this->email->to('foo.900.fch@gmail.com'); 
            //$this->email->cc('another@another-example.com'); 
            //$this->email->bcc('them@their-example.com'); 

            $this->email->subject('Email Test');
            $this->email->message('Testing the email class.');  

            if($this->email->send())
            {
                echo 'Email sent.';
            }
            else
            {
                show_error($this->email->print_debugger());
            }
        }
     }

在config.php(配置文件夹)中,我有:

$config['smtp_host'] = 'ssl://smtp.gmail.com'; // SMTP Server Address.
$config['smtp_port'] = '465'; // SMTP Port.
$config['protocol'] ='sendmail';

2 个答案:

答案 0 :(得分:2)

您必须在系统中配置sendmail。

如果您使用的是ubuntu,请按照以下步骤操作: 如果您想在Ubuntu-Linux服务器上将Gmail帐户用作免费的SMTP服务器,您会发现本文很有用。本指南使用Ubuntu 12.04进行测试。如果您遇到任何问题,请随时使用评论部分。 通过smtp.gmail.com转发Postfix邮件:

首先,安装所有必要的软件包:

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

如果之前没有安装postfix,postfix配置向导会问你一些问题。只需选择您的服务器作为Internet站点,并使用类似mail.example.com

的FQDN

然后打开你的postfix配置文件:

vim /etc/postfix/main.cf

以及以下内容:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

您可能已经注意到我们未在上面的行中指定我们的Gmail用户名和密码。他们将进入另一个文件。打开/创建

vim /etc/postfix/sasl_passwd

并添加以下内容:

[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

如果您想使用Google App的域名,请将@ gmail.com替换为您的@ domain.com

修复权限并更新postfix配置以使用sasl_passwd文件:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

接下来,验证证书以避免遇到错误。只需运行以下命令:

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

最后,重新加载postfix配置以使更改生效:

sudo /etc/init.d/postfix reload

测试 检查邮件是否通过Gmail SMTP服务器发送

如果您已正确配置所有内容,则以下命令应生成从服务器到邮箱的测试邮件。

echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com

有关详情:https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

感谢rtcamp.com,它给了我很多帮助: - )

答案 1 :(得分:0)

ssl代码需要SMTP的Google邮件,因此open_ssl应在php.ini

中启用扩展程序

Codeigniter邮件库有关SSL / TLS click here

的更多详细信息

在php中启用OPEN SSL click here

相关问题