How To Send Email Using Html Templates In Codeigniter
Reading Fourth dimension: nine minutes
one,322 Views
Sending Emails with SMTP in any application nowadays very mutual. In CodeIgniter 4, we have in-built Email library class provided. So, inside this article nosotros will see CodeIgniter 4 send email using Custom Template using SMTP configuration.
The procedure of SMTP configuration to send emails is very unproblematic. Nosotros need to configure settings and by the help of which nosotros send emails.
We will use view() method to add a custom template. Inside this article we volition encounter how to use a custom template to transport emails in a proper format.
Learn More than –
- CodeIgniter iv Send Email with Attachments Tutorial
- CodeIgniter 4 Ship Email with Multiple Attachments
- CodeIgniter 4 Send Email with SMTP Tutorial
- CodeIgniter 4 Ship Push Notification to IOS Using Firebase
Let'due south get started.
What is Email Class in CodeIgniter four?
Email class is a in-congenital library. There are several features available by the help of which we configure emails. CodeIgniter 4 email class have all these features
- Multiple Protocols: Mail, Sendmail, and SMTP
- TLS and SSL Encryption for SMTP
- Multiple recipients
- CC and BCCs
- HTML or Plaintext e-mail
- Attachments
- Give-and-take wrapping
- Priorities
- BCC Batch Mode, enabling large email lists to exist broken into small BCC batches.
- Email Debugging tools
CodeIgniter 4 Installation
To create a CodeIgniter iv setup run this given command into your vanquish or terminal. Please make certain composer should be installed.
$ composer create-project codeigniter4/appstarter codeigniter-4 Assuming you have successfully installed application into your local system.
.env Setup
When we install CodeIgniter 4, we will have env file at root. To utilise the environment variables means using variables at global scope we demand to do env to .env
Either we can do via renaming file every bit uncomplicated as that. Likewise we can do by last command.
Open project in concluding
$ cp env .env In a higher place control volition create a re-create of env file to .env file.
At present we are ready to employ environment variables.
Enable Development Style
CodeIgniter starts up in product mode by default. You lot need to make information technology in development mode to see whatsoever mistake if you are working with application.
Open .env file from root.
# CI_ENVIRONMENT = product // Do it to CI_ENVIRONMENT = development At present application is in evolution manner.
Email Settings Configuration
For email configuration settings, we tin can use whatsoever sending email protocol like mail service, sendmail or smtp.
Here, nosotros will use SMTP and utilise Host as Gmail.
Open the Email.php file from /app/Config folder.
<?php namespace Config; utilise CodeIgniter\Config\BaseConfig; grade Email extends BaseConfig { /** * @var string */ public $fromEmail = "xxxxx@gmail.com"; /** * @var cord */ public $fromName = "Online Web Tutor Support"; /** * @var cord */ public $recipients; /** * The "user agent" * * @var string */ public $userAgent = 'CodeIgniter'; /** * The postal service sending protocol: mail, sendmail, smtp * * @var string */ public $protocol = 'smtp'; /** * The server path to Sendmail. * * @var string */ public $mailPath = '/usr/sbin/sendmail'; /** * SMTP Server Address * * @var string */ public $SMTPHost = "smtp.gmail.com"; /** * SMTP Username * * @var string */ public $SMTPUser = "xxxxx@gmail.com"; /** * SMTP Password * * @var cord */ public $SMTPPass = "xxxxxxx"; /** * SMTP Port * * @var integer */ public $SMTPPort = 587; /** * SMTP Timeout (in seconds) * * @var integer */ public $SMTPTimeout = threescore; /** * Enable persistent SMTP connections * * @var boolean */ public $SMTPKeepAlive = false; /** * SMTP Encryption. Either tls or ssl * * @var string */ public $SMTPCrypto = 'tls'; /** * Enable discussion-wrap * * @var boolean */ public $wordWrap = true; /** * Character count to wrap at * * @var integer */ public $wrapChars = 76; /** * Blazon of mail, either 'text' or 'html' * * @var string */ public $mailType = 'html'; /** * Character set (utf-eight, iso-8859-1, etc.) * * @var string */ public $charset = 'UTF-8'; /** * Whether to validate the email address * * @var boolean */ public $validate = fake; /** * E-mail Priority. one = highest. v = everyman. iii = normal * * @var integer */ public $priority = 3; /** * Newline graphic symbol. (Use "\r\n" to comply with RFC 822) * * @var string */ public $CRLF = "\r\northward"; /** * Newline character. (Utilise "\r\n" to comply with RFC 822) * * @var string */ public $newline = "\r\n"; /** * Enable BCC Batch Way. * * @var boolean */ public $BCCBatchMode = faux; /** * Number of emails in each BCC batch * * @var integer */ public $BCCBatchSize = 200; /** * Enable notify message from server * * @var boolean */ public $DSN = false; } Update $mailType
public $mailType = 'html'; Hither, inside this file nosotros have configured all basic settings for sending emails. But i more matter earlier setting your email address and password to a SMTP details. Y'all need to make information technology equally for LESS SECURE APPS AT YOUR GOOGLE ACCOUNT.
When you go at your given email account settings. You will observe a Security Tab. When you click into it –
Next, nosotros demand to create a method in controller and call Email service.
Add together Route
Open up Routes.php from /app/Config folder. Add given route into it.
//... $routes->get("transport-post", "UserController::sendMail"); //... Employ & Send Email Using Library
We need to create a controller.
Controller Setup
Open projection into terminal and run this spark command to create controller.
$ php spark make:controller User --suffix Information technology will create a file UserController.php inside /app/Controllers folder.
Open UserController.php file and write this code.
<?php namespace App\Controllers; apply App\Controllers\BaseController; grade UserController extends BaseController { public function sendMail() { $email = \Config\Services::email(); // loading for use $email->setTo("xxxxxx@gmail.com"); $email->setSubject("Examination Post with Template"); // Using a custom template $template = view("email-template", []); $email->setMessage($template); // Send email if ($email->ship()) { echo 'Email successfully sent, please check.'; } else { $data = $electronic mail->printDebugger(['headers']); print_r($information); } } } Create Custom Email Template
Create a view file electronic mail-template.php within /app/Views folder. This will be the custom email template.
Open up e-mail-template.php and write this following code into information technology.
<html> <head> <title>Email Using a Custom Template</title> </head> <body> <h3>Test Mail - Back up</h3> <p> This is a sample message sent by Online Spider web Tutor Back up using a custom template. </p> <p> Thanks,<br />Online Spider web Tutor </p> </body> </html>
Awarding Testing
Beginning development server:
$ php spark serve URL: http://localhost:8080/transport-mail
Output
Email successfully sent, delight check.
We promise this article helped you to learn CodeIgniter 4 Send Email Using Custom Template Tutorial in a very detailed way.
Buy Me a Coffee
Online Web Tutor invites you to endeavor Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter four, Node Js, etc into a depth level. Chief the Coding Skills to Go an Expert in Spider web Development. Then, Search your favourite form and enroll at present. Click here to join.
If you liked this commodity, then delight subscribe to ourYouTube Channel for PHP & it's framework, WordPress, Node Js video tutorials. Yous can also find usa onTwitter andFacebook.
How To Send Email Using Html Templates In Codeigniter,
Source: https://onlinewebtutorblog.com/codeigniter-4-send-email-using-custom-template/
Posted by: mcdanielcalluser.blogspot.com

0 Response to "How To Send Email Using Html Templates In Codeigniter"
Post a Comment