SMTP For Email Sending

As you know we created a custom ajax form previously. In this lecture, we going to learn how to avoid sending emails to junk and spam boxes by Simple Mail Transfer Protocol (SMTP). We will create our own custom mailer function by using phpmailer_init hook location.

STMP Requirments
We have to know about the below things to set up SMTP

  • SetFrpm
  • Host
  • Port
  • SMTPAuth
  • SMTPSecure
  • Username
  • Password
  • IsSMTP method

You also use your Gmail for STMP authorization. but first, we will be talking about Cpanel then Gmail. Both codes are same just the Host will be changed in both scenarios.

Before going further, we define your webmail email and password in wordpress config.php file where our database details are.

Config File
Add the below code in the config.php file.

define('SMTP_LOGIN','example@domain.com');
define('SMTP_PASSWORD','example_password');

 

STMP Function
Add below code in the functions.php file and change SetFrom and Host to your correct information.

add_action('phpmailer_init','custom_mailer');
function custom_mailer( PHPMailer $phpmailer){
    
    $phpmailer->SetFrom('example@domain.com','Developer');
    $phpmailer->Host = 'Add Outgoing Server here';
    $phpmailer->Port = 465;
    $phpmailer-> SMTPAuth = true;
    $phpmailer->SMTPSecure = 'tls';
    $phpmailer->Username = SMTP_LOGIN;
    $phpmailer->Password = SMTP_PASSWORD;
    $phpmailer->IsSMTP();

}

 

#BeingCodeStuffer

Related Theme Developments