We will follow 3 simple step
- SMTP Setting in CodeIgniter 4
- Load Email service/library in CodeIgniter 4
- Send mail in CodeIgniter 4
Here we can use Custom HTML Template and Attachment in CodeIgniter 4.
STEP 1: SMTP Setting
Open /app/Config/Email.php and set values. Example given below which i used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | <?php namespace Config; use CodeIgniter\Config\BaseConfig; class Email extends BaseConfig { public string $fromEmail = ''; public string $fromName = ''; public string $recipients = ''; /** * The "user agent" */ public string $userAgent = 'PHPMailer'; /** * The mail sending protocol: mail, sendmail, smtp */ public string $protocol = 'mail'; /** * The server path to Sendmail. */ public string $mailPath = '/usr/sbin/sendmail'; /** * SMTP Server Address */ public string $SMTPHost = 'mail.yoursite.com'; /** * SMTP Username */ public string $SMTPUser = 'no-reply@yoursite.com'; /** * SMTP Password */ public string $SMTPPass = '***password'; /** * SMTP Port */ public int $SMTPPort = 587; /** * SMTP Timeout (in seconds) */ public int $SMTPTimeout = 30; /** * Enable persistent SMTP connections */ public bool $SMTPKeepAlive = false; /** * SMTP Encryption. Either tls or ssl */ public string $SMTPCrypto = 'ssl'; /** * Enable word-wrap */ public bool $wordWrap = true; /** * Character count to wrap at */ public int $wrapChars = 76; /** * Type of mail, either 'text' or 'html' */ public string $mailType = 'html'; /** * Character set (utf-8, iso-8859-1, etc.) */ public string $charset = 'UTF-8'; /** * Whether to validate the email address */ public bool $validate = true; /** * Email Priority. 1 = highest. 5 = lowest. 3 = normal */ public int $priority = 3; /** * Newline character. (Use “\r\n” to comply with RFC 822) */ public string $CRLF = "\n"; /** * Newline character. (Use “\r\n” to comply with RFC 822) */ public string $newline = "\n"; /** * Enable BCC Batch Mode. */ public bool $BCCBatchMode = false; /** * Number of emails in each BCC batch */ public int $BCCBatchSize = 200; /** * Enable notify message from server */ public bool $DSN = false; } |
STEP 2 : Load Email service/library
You can load in __construct or in your Controller method, but here i will load in __construct, because we will need this in whole controller. find example below.
1 2 3 4 | public function __construct() { $this->email = \Config\Services::email(); $this->parser = service('renderer'); } |
STEP 3 : Complete code of Controller Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public function email(){ $datas['name'] = 'CodyPaste'; $template = view('email-template/testMail',$datas); $this->email->setFrom('no-reply@yoursite.com', 'CodyPaste'); $this->email->setTo('codypaste@gmail.com'); $this->email->setSubject('Mail Test'); $this->email->setMessage($template);//your message here $this->email->setCC('another@emailHere');//CC $this->email->setBCC('thirdEmail@emialHere');// and BCC $filename = '/img/yourPhoto.jpg'; //you can use the App patch $this->email->attach($filename); $send = $this->email->send(); if($send){ echo"Mail sent"; }else{ $data = $this->email->printDebugger(['headers']); print_r($data); } } |