How to use PHPMailer on a server hosted on cPanel ?

 To use PHPMailer on a server hosted on cPanel, you can follow these steps:


1. **Download PHPMailer:**

   Go to the PHPMailer GitHub repository (https://github.com/PHPMailer/PHPMailer) and download the latest release. Extract the contents of the ZIP file.


2. **Upload PHPMailer to your cPanel:**

   Upload the PHPMailer files to your cPanel account. You can use an FTP client or cPanel's File Manager for this. Place the PHPMailer files in a directory within your web server's public_html directory.


3. **Configure PHPMailer:**

   Create a new PHP file (e.g., `send_email.php`) where you'll configure and use PHPMailer to send emails. Open the PHP file and include the necessary PHPMailer files. Here's a basic example:


   ```php

   <?php

   use PHPMailer\PHPMailer\PHPMailer;

   use PHPMailer\PHPMailer\Exception;


   require 'path/to/PHPMailer/src/Exception.php';

   require 'path/to/PHPMailer/src/PHPMailer.php';

   require 'path/to/PHPMailer/src/SMTP.php';


   $mail = new PHPMailer(true); // true enables exceptions


   try {

       // Server settings

       $mail->isSMTP(); // Set mailer to use SMTP

       $mail->Host = 'your-smtp-server.com'; // Specify main and backup SMTP servers

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

       $mail->Username = 'your-email@example.com'; // SMTP username

       $mail->Password = 'your-email-password'; // SMTP password

       $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted

       $mail->Port = 587; // TCP port to connect to


       // Recipients

       $mail->setFrom('your-email@example.com', 'Your Name');

       $mail->addAddress('recipient@example.com', 'Recipient Name'); // Add a recipient


       // Content

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

       $mail->Subject = 'Subject';

       $mail->Body = 'This is the HTML message body';


       $mail->send();

       echo 'Message has been sent';

   } catch (Exception $e) {

       echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

   }

   ```


   Replace `'path/to/PHPMailer/src/'` with the actual path where you uploaded PHPMailer, and update the SMTP settings, sender's and recipient's email addresses, and other configuration options.


4. **Configure SMTP Settings:**

   Replace the SMTP settings in the PHP file with your own SMTP server details. You can obtain these details from your hosting provider or cPanel account. Common SMTP servers include "smtp.yourdomain.com" or "mail.yourdomain.com."


5. **Run the PHP script:**

   Access the PHP script through your web browser by visiting `http://yourdomain.com/send_email.php`. If there are any issues, error messages will be displayed to help you troubleshoot.


Keep in mind that security is crucial when dealing with email and passwords. Always store sensitive information securely, and avoid hardcoding credentials in your scripts. Additionally, consider using environment variables or configuration files for storing sensitive information.

Comments