勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

CodeIgniter4でのメール送信方法(テキスト・HTMLメール)

  PHP CodeIgniter

CodeIgniter4.4.4でテキストメール及びHTMLメールを送信したい。ついでにファイル添付、CC・BCC機能もあれば本番案件でも使えそう。以下に実装方法をメモ。

 

リファレンス

https://codeigniter.com/user_guide/libraries/email.html

 

ソースコード

テキストメール

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$email = \Config\Services::email();
$email->setFrom('from@test.com', 'テスト送信者名');
$email->setTo('to@test.com');
$email->setSubject('テストタイトル');
$email->setMessage('テスト本文');
$email->send();
$email = \Config\Services::email(); $email->setFrom('from@test.com', 'テスト送信者名'); $email->setTo('to@test.com'); $email->setSubject('テストタイトル'); $email->setMessage('テスト本文'); $email->send();
$email = \Config\Services::email();
$email->setFrom('from@test.com', 'テスト送信者名');
$email->setTo('to@test.com');
$email->setSubject('テストタイトル');
$email->setMessage('テスト本文');
$email->send();

テキストメール(ファイル添付、CC・BCC機能付き)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$email = \Config\Services::email();
$email->setFrom('from@test.com', 'テスト送信者名');
$email->setTo('to@test.com');
$email->setCC('cc@test.com');
$email->setBCC('bcc@test.com');
$email->setSubject('テストタイトル');
$email->setMessage('テスト本文');
$email->attach('/var/www/html/test.jpg');
$email->send();
$email = \Config\Services::email(); $email->setFrom('from@test.com', 'テスト送信者名'); $email->setTo('to@test.com'); $email->setCC('cc@test.com'); $email->setBCC('bcc@test.com'); $email->setSubject('テストタイトル'); $email->setMessage('テスト本文'); $email->attach('/var/www/html/test.jpg'); $email->send();
$email = \Config\Services::email();
$email->setFrom('from@test.com', 'テスト送信者名');
$email->setTo('to@test.com');
$email->setCC('cc@test.com');
$email->setBCC('bcc@test.com');
$email->setSubject('テストタイトル');
$email->setMessage('テスト本文');
$email->attach('/var/www/html/test.jpg');
$email->send();

HTMLメール

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$email = \Config\Services::email();
//メールタイプをHTMLに変更
$mail_config['mailType'] = 'html';
$email->initialize($mail_config);
$email->setFrom('from@test.com', 'テスト送信者名');
$email->setTo('to@test.com');
$email->setSubject('テストタイトル');
$body = '<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTMLmail</title>
</head>
<body>
<h1>HTML mail test</h1>
</body>
</html>
';
$email->setMessage($body);
$email->send();
$email = \Config\Services::email(); //メールタイプをHTMLに変更 $mail_config['mailType'] = 'html'; $email->initialize($mail_config); $email->setFrom('from@test.com', 'テスト送信者名'); $email->setTo('to@test.com'); $email->setSubject('テストタイトル'); $body = '<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>HTMLmail</title> </head> <body> <h1>HTML mail test</h1> </body> </html> '; $email->setMessage($body); $email->send();
$email = \Config\Services::email();

//メールタイプをHTMLに変更
$mail_config['mailType'] = 'html';
$email->initialize($mail_config);

$email->setFrom('from@test.com', 'テスト送信者名');
$email->setTo('to@test.com');
$email->setSubject('テストタイトル');
$body = '<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTMLmail</title>
</head>
<body>
<h1>HTML mail test</h1>
</body>
</html>
';
$email->setMessage($body);
$email->send();

 

エラー判定

リファレンスによるとsend()の戻り値で判定できる模様。そのため以下のような形で分岐処理させる。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if( $email->send() ){
//送信成功時の処理
}else{
//送信失敗時の処理
}
if( $email->send() ){ //送信成功時の処理 }else{ //送信失敗時の処理 }
if( $email->send() ){
    //送信成功時の処理
}else{
    //送信失敗時の処理
}

 

所感

メール関連は特に問題無さそうなのでフォームと組み合わせればお問い合わせページとか作れそう。

 - PHP CodeIgniter

  関連記事

CodeIgniter4 & jQueryを用いて同一サイト内でajax通信する方法

CodeIgniter4.4.4 & jQueryを用いて同一サイト内で ...

CodeIgniter3でログをファイルに出力する方法

Codeigniter3でエラーログの設定を行い特定のファイルに出力させ当該ログ ...

DTIのVPSにCodeigniter3を導入する方法

DTIのVPSにPHPフレームワークのCodeigniter3系をインストールし ...

CodeIgniter4で祝日一覧APIにCURLでリクエストし結果をファイルキャッシュする方法

Codeigniter4.4.4で祝日一覧APIにCURLでリクエストしたい。尚 ...

CodeIgniter4&Bootstrap&jQueryで簡易版お問い合わせページの作成

CodeIgniter4.4.4&Bootstrap&jQuer ...

S