[英]Send email in CodeIgniter
我是CodeIgniter的新手。 我是否知道如何通過localhost在CodeIgniter中發送電子郵件? 我應該把什么在smtp_user
和smtp_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';
您必須在系統中配置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,它給了我很多幫助:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.