• Call Us: +92-333-7276335
  • - Mail Us: info@shekztech.com

Plot 1177, Sector 31B - Crossing, Karachi, Sindh

Loading...
How can you send email from localhost to server in codeigniter 4
  • Views: 4.7K
  • Category: Codeigniter
  • Published at: 23 Apr, 2020
  • Updated at: 23 Aug, 2023

How can you send email from localhost to server in codeigniter 4

How can you send email from localhost to server in Codeigniter 4

Sending emails in Codeigniter 4 is very easy because of the new functionality; now, the stable version of Codeigniter is available to download from the official website.

They have introduced the services classes to use the Codeigniter 4 services like email; you call the services in Codeigniter by using this code.

Make sure I have created the email using Cpanel and use the same email and password here in this article.

\Config\Services::serviceName();
// i.e  \Config\Services::email();

In this segment, you should know how to use it; you can send the email from your website/localhost to the user/client using Codeigniter 4, so before using the email library, you need to see the library setting in Codeigniter 4.

Step 1: Go to the app/config/Email.php and open the file

Step 2: Update the Email set by this code.

<?php
namespace Config;

use CodeIgniter\Config\BaseConfig;

class Email extends BaseConfig
{

  /**
   * @var string
   */
  public $fromEmail;

  /**
   * @var string
   */
  public $fromName;

  /**
   * @var string
   */
  public $recipients;

  /**
   * The "user agent"
   *
   * @var string
   */
  public $userAgent = 'CodeIgniter';

  /**
   * The mail 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='mail.shekztech.com';

  /**
   * SMTP Username
   *
   * @var string
   */
  public $SMTPUser = 'abcd@shekztech.com';

  /**
   * SMTP Password
   *
   * @var string
   */
  public $SMTPPass = 'YourPasswordHere

  Step 3: Now go to your controller/method and just use/call the library/class

public function yourMethod(){
   $message = "Please activate the account ".anchor('user/activate/'.$data['u_link'],'Activate Now','');
   $email = \Config\Services::email();
   $email->setFrom('ci4signup@shekztech.com', 'your Title Here');
   $email->setTo($data['u_email']);
   $email->setSubject('Your Subject here | shekztech.com');
   $email->setMessage($message);//your message here

   $email->setCC('another@emailHere');//CC
   $email->setBCC('thirdEmail@emialHere');// and BCC
   $filename = '/img/yourPhoto.jpg'; //you can use the App patch 
   $email->attach($filename);
   
   $email->send();
   $email->printDebugger(['headers']);
}


<?php
namespace Config;

use CodeIgniter\Config\BaseConfig;

class Email extends BaseConfig
{

   /**
    * @var string
    */
   public $fromEmail;

   /**
    * @var string
    */
   public $fromName;

   /**
    * @var string
    */
   public $recipients;

   /**
    * The "user agent"
    *
    * @var string
    */
   public $userAgent = 'CodeIgniter';

   /**
    * The mail 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='mail.shekztech.com';

   /**
    * SMTP Username
    *
    * @var string
    */
   public $SMTPUser = 'ci4signup@shekztech.com';

   /**
    * SMTP Password
    *
    * @var string
    */
   public $SMTPPass = 'ci4signup123$';

   /**
    * SMTP Port
    *
    * @var integer
    */
   public $SMTPPort = 587;

   /**
    * SMTP Timeout (in seconds)
    *
    * @var integer
    */
   public $SMTPTimeout = 15;

   /**
    * Enable persistent SMTP connections
    *
    * @var boolean
    */
   public $SMTPKeepAlive = false;

   /**
    * SMTP Encryption. Either tls or ssl
    *
    * @var string
    */
   public $SMTPCrypto = 'tls';

   /**
    * Enable word-wrap
    *
    * @var boolean
    */
   public $wordWrap = true;

   /**
    * Character count to wrap at
    *
    * @var integer
    */
   public $wrapChars = 76;

   /**
    * Type of mail, either 'text' or 'html'
    *
    * @var string
    */
   public $mailType = 'html';

   /**
    * Character set (utf-8, iso-8859-1, etc.)
    *
    * @var string
    */
   public $charset = 'UTF-8';

   /**
    * Whether to validate the email address
    *
    * @var boolean
    */
   public $validate = false;

   /**
    * Email Priority. 1 = highest. 5 = lowest. 3 = normal
    *
    * @var integer
    */
   public $priority = 3;

   /**
    * Newline character. (Use “\r\n” to comply with RFC 822)
    *
    * @var string
    */
   public $CRLF = "\r\n";

   /**
    * Newline character. (Use “\r\n” to comply with RFC 822)
    *
    * @var string
    */
   public $newline = "\r\n";

   /**
    * Enable BCC Batch Mode.
    *
    * @var boolean
    */
   public $BCCBatchMode = false;

   /**
    * Number of emails in each BCC batch
    *
    * @var integer
    */
   public $BCCBatchSize = 200;

   /**
    * Enable notify message from server
    *
    * @var boolean
    */
   public $DSN = false;

}

 

Step 3: Now go to your controller/method and use/call the library/class


 

https://www.youtube.com/watch?v=zanP_H_RgSo

Shehzad Ahmed

Shehzad Ahmed is a highly qualified expert with a Master of Philosophy in Computer Science and a decade of extensive industry experience. With his impressive track record in web development and computer science, he has not only left an indelible mark on the industry but also made substantial contributions to education. Since , he has created more than eighty exhaustive courses, paving the way for innumerable individuals interested in learning and development. His unparalleled knowledge and innate ability to elucidate complex ideas make him a highly sought-after educator and consultant. Choose Shehzad and take advantage of his exceptional balance of technical expertise and teaching prowess to propel your learning journey or project to new heights.

0 Comment(s)
Write your comment