Codeigniter-sending-email
CodeIgniter-メールの送信
CodeIgniterでメールを送信するのははるかに簡単です。 CodeIgniterで電子メールに関する設定も構成します。 CodeIgniterは、メールを送信するための次の機能を提供します-
- 複数のプロトコル-メール、Sendmail、およびSMTP
- SMTPのTLSおよびSSL暗号化
- 複数の受信者
- CCおよびBCC
- HTMLまたはプレーンテキストメール
- 添付ファイル
- ワードラッピング
- 優先順位
- BCCバッチモード。大きな電子メールリストを小さなBCCバッチに分割できます。 *メールデバッグツール
電子メールクラスには、電子メールの送信作業を簡素化する次の機能があります。
S.N. | Syntax | Parameters | Return | Return Type |
---|---|---|---|---|
1 | from($from[, $name = ''[, $return_path = NULL]]) |
|
CI_Email instance (method chaining) | CI_Email |
2 | reply_to($replyto[, $name = '']) |
$replyto (string) − E-mail address for replies
|
CI_Email instance (method chaining) | CI_Email |
2 | to($to) | $to (mixed) − Comma-delimited string or an array of e-mail addresses | CI_Email instance (method chaining) | CI_Email |
3 | cc($cc) | *$cc *(mixed) − Comma-delimited string or an array of e-mail addresses | CI_Email instance (method chaining) | CI_Email |
4 | bcc($bcc[, $limit = '']) |
|
CI_Email instance (method chaining) | CI_Email |
5 | subject($subject) | $subject (string) − E-mail subject line | CI_Email instance (method chaining) | CI_Email |
6 | message($body) | $body (string) − E-mail message body | CI_Email instance (method chaining) | CI_Email |
7 | set_alt_message($str) | *$str *(string) − Alternative e-mail message body | CI_Email instance (method chaining) | CI_Email |
8 | set_header($header, $value) |
|
CI_Email instance (method chaining) | CI_Email |
9 | clear([$clear_attachments = FALSE]) | $clear_attachments (bool) – Whether or not to clear attachments | CI_Email instance (method chaining) | CI_Email |
10 | send([$auto_clear = TRUE]) | *$auto_clear *(bool) − Whether to clear message data automatically | CI_Email instance (method chaining) | CI_Email |
11 | attach($filename[, $disposition = [, $newname = NULL[, $mime = ]]]) |
|
CI_Email instance (method chaining) | CI_Email |
12 | attachment_cid($filename) | *$filename *(string) − Existing attachment filename | Attachment Content-ID or FALSE if not found | string |
メールを送る
CodeIgniterを使用して電子メールを送信するには、まず、次を使用して電子メールライブラリをロードする必要があります-
$this->load->library('email');
ライブラリをロードした後、次の関数を実行して、電子メールを送信するために必要な要素を設定します。* from()関数は、電子メールの送信元および to()関数の使用元を設定するために使用されます-電子メールの送信先。 * subject()*および message()*関数は、電子メールの件名とメッセージを設定するために使用されます。
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
その後、以下に示す* send()*関数を実行してメールを送信します。
$this->email->send();
例
コントローラーファイル Email_controller.php を作成し、 application/controller/Email_controller.php に保存します。
<?php
class Email_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
}
public function index() {
$this->load->helper('form');
$this->load->view('email_form');
}
public function send_mail() {
$from_email = "your@example.com";
$to_email = $this->input->post('email');
//Load email library
$this->load->library('email');
$this->email->from($from_email, 'Your Name');
$this->email->to($to_email);
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
//Send mail
if($this->email->send())
$this->session->set_flashdata("email_sent","Email sent successfully.");
else
$this->session->set_flashdata("email_sent","Error in sending Email.");
$this->load->view('email_form');
}
}
?>
*email_form.php* というビューファイルを作成し、 *application/views/email_form.php* に保存します。
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter Email Example</title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Email_controller/send_mail');
?>
<input type = "email" name = "email" required/>
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
</html>
*application/config/routes.php* の *routes.php* ファイルに変更を加え、ファイルの最後に次の行を追加します。
$route['email'] = 'Email_Controller';
次のリンクにアクセスして、上記の例を実行します。 yoursite.comをサイトのURLに置き換えます。
http://yoursite.com/index.php/email