Live Chat

Domain Scan

(empty)

Login


How to Send Automated Emails from Your Website?
(24-jun-2024)

Configuring PHPMailer SMTP for Automated Emails on cPanel

SMTP (Simple Mail Transfer Protocol) is a standard communication protocol for sending emails across the Internet. It enables the transmission of messages from an email client to an email server or between email servers, ensuring the smooth delivery of emails. PHPMailer is a popular PHP library that provides a user-friendly interface for sending emails via SMTP, offering enhanced functionality and ease of use compared to the native PHP mail() function. In this guide, we will walk through the steps to configure PHPMailer SMTP for your email address on a cPanel server, enabling you to send automated emails from your website.

Step-by-Step Guide to Configuring PHPMailer SMTP for Your Email Address

1. Create an Email Address
First, create an email address under your domain (e.g., admin@yourdomain.com) in the cPanel Email Accounts section. If you already have an email account that you intend to use, you can obtain the necessary SMTP configuration details by navigating to Connect Devices next to the relevant email address.
(You can check out our previous blog, "Set Up Email Accounts for Your Domain Using cPanel" for a detailed guide on creating professional email accounts in cPanel.)

2. Set Up the Directory Structure
Next, create a folder named phpmail within your cPanel File Manager's public_html directory. Inside the phpmail folder, create two additional folders named config and mailscript. Download the PHPMailer library from the PHPMailer GitHub repository and place the extracted files into the phpmail folder. The directory structure should look like this:
  • /home/USERNAME/public_html/phpmail/config
  • /home/USERNAME/public_html/phpmail/PHPMailer
  • /home/USERNAME/public_html/phpmail/mailscript
  • (If you place the files in a different location, ensure you update the $directoryPath variable in the relevant scripts to reflect the correct file paths.)

    3. Add SMTP Credentials
    Create a file named .smtpconfig inside the config folder and add the following code:
      smtp_debug = 0
      smtp_host = smtpoutgoingserver.com
      smtp_port = 465
      smtp_secure = ssl
      smtp_username = admin@yourdomain.com
      smtp_password = your-email-password
      email_from = admin@yourdomain.com
      email_from_name = Admin
    
    Modify the fields with the appropriate SMTP credentials for your email address. If hidden files are not enabled in cPanel, select Show Hidden Files (dotfiles) in Settings to view and edit this file.

    4. Extract Mail Credentials
    Create a file named smtpconfig.php inside the config folder and add the following PHP code to extract mail credentials from the hidden configuration file:
    <?php
    
    $directoryPath = dirname(dirname(__DIR__));
    
    $configFilePath = $directoryPath . '/phpmail/config/.smtpconfig';
    
    // Read and parse the hidden config file
    $config = parse_ini_file($configFilePath);
    
    // Extract values from the config array
    $smtpdebug = $config['smtp_debug'];
    $smtphost = $config['smtp_host'];
    $smtpport = $config['smtp_port'];
    $smtpsecure = $config['smtp_secure'];
    $smtpUsername = $config['smtp_username'];
    $smtpPassword = $config['smtp_password'];
    $emailFrom = $config['email_from'];
    $emailFromName = $config['email_from_name'];
    
    ?>
    

    5. Test SMTP Configuration and Send an Email
    Create a file named testmail.php inside the mailscript folder and add the following PHP code to test the email sending functionality:
    <?php
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    $directoryPath = dirname(dirname(__DIR__));
    
    require $directoryPath . '/phpmail/PHPMailer/src/Exception.php';
    require $directoryPath . '/phpmail/PHPMailer/src/PHPMailer.php';
    require $directoryPath . '/phpmail/PHPMailer/src/SMTP.php';
    
    include $directoryPath . '/phpmail/config/smtpconfig.php';
    
    // Set up PHPMailer
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = $smtpdebug;
    $mail->Host = $smtphost;
    $mail->Port = $smtpport;
    $mail->SMTPSecure = $smtpsecure;
    $mail->SMTPAuth = true;
    $mail->Username = $smtpUsername;
    $mail->Password = $smtpPassword;
    $mail->setFrom($emailFrom, $emailFromName);
    
    // Test Mail
    $mail->addAddress("test@domain.com", "Test Mail");
    $mail->Subject = 'PHPMailer SMTP Test';
    $mail->msgHTML("This is a test mail");
    
    if(!$mail->send()){
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    
    ?>
    
    Replace test@domain.com with the recipient's email address and open https://yourdomain.com/phpmail/mailscript/testmail.php in your browser to check the email sending functionality by sending a test mail. If the mail is sent successfully, it will show Message sent! on the webpage.

    Use Cases for PHPMailer SMTP Configuration
    Configuring PHPMailer SMTP on your cPanel server allows you to send authenticated emails directly from your website. This setup can be used in various scenarios, such as:
  • Contact Forms: Automatically send confirmation emails to users who submit inquiries through your website's contact form.
  • Registration Confirmation: Send automated emails to users upon successful registration on your website, providing verification or welcome messages.
  • Password Resets: Implement password reset functionality by sending secure, automated emails to users who request to reset their passwords.
  • Order Confirmations: For e-commerce websites, send order confirmation emails to customers upon successful purchase.

  • Conclusion
    By following these steps, you can effectively configure PHPMailer SMTP on your cPanel server to send automated emails from your website. This setup ensures reliable and authenticated email delivery, enhancing the professionalism and security of your email communications.

    With this guide, you'll have everything set up in no time, making your website even more efficient and user-friendly. Happy emailing!


    Written by: Register.lk Systems Hero - Lakshani
    BACK 2 BLOG