Hosting email address send SMTP email in PHP

In this tutorial, we will teach you how to send email from a hosting email address using SMTP in PHP. Recently, Google has disabled its “less secure apps” option. This means that you can no longer send SMTP emails from your Gmail account.


So, today we are going to show you, how you can send SMTP emails from your own hosting email address.

Step 1: Login to cPanel

First step, is to login to your hosting provider and open cPanel. In your cPanel, search for “email” and goto “Email accounts”.

Step 2: Search email address

Then search for desired email address, from which you want to send emails and select “Check email”.

Step 3: Open mail client configurations

On the next page, you need to look for the section “Other webmail features” and click on the “Configure mail clients” box.

Step 4: Check SMTP details of your hosting email address

From here, you need to loop for the following configurations.

Step 5: Send SMTP email

Now, we need to install a library called PHPMailer. Open the command prompt at the root of your project and run the following command:

> composer require phpmailer/phpmailer

Make sure you have downloaded the composer from here. Write the following code in the file from where you want to send the email:

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try
{
    //Enable verbose debug output
    $mail->SMTPDebug = 0; // SMTP::DEBUG_SERVER;

    //Send using SMTP
    $mail->isSMTP();

    //Set the SMTP server to send through
    $mail->Host = "mail host address";

    //Enable SMTP authentication
    $mail->SMTPAuth = true;

    //SMTP username
    $mail->Username = "email address";

    //SMTP password
    $mail->Password = "password";

    //Enable TLS encryption;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    $mail->Port = 587;

    //Recipients
    $mail->setFrom("email address", "your name");

    //Add a recipient
    $mail->addAddress("recipient email address", "recipient name");

    //Set email format to HTML
    $mail->isHTML(true);

    $mail->Subject = "subject";
    $mail->Body    = "body";

    $mail->send();
    echo 'Message has been sent';
}
catch (Exception $e)
{
    die("Message could not be sent. Mailer Error: " . $mail->ErrorInfo);
}

So that’s it. That’s how you can send emails from your hosting email address using SMTP in PHP. If you want to attach files with the email, we have already created a separate tutorial on that. If you face any problems in following this, kindly do let me know.

2 Replies to “Hosting email address send SMTP email in PHP”

  1. This way of sending email doesn’t work anymore because there is no lower option to set “less secure apps access” is there any other way since everyone has problems with this ?

    1. Actually, this post is an alternative to “less secure apps access”. Since using this method, you will be able to send email via your own web hosting SMTP, instead of using Gmail.

Comments are closed.