How to install php mailer with composer

To install PHPMailer using Composer, you can follow these steps:

  1. Open your terminal or command prompt and navigate to your project directory.

  2. Run the following command to install PHPMailer using Composer:

    composer require phpmailer/phpmailer

    This will download and install the latest version of PHPMailer.

  3. Once the installation is complete, you can verify that PHPMailer has been installed by running the following command:

    composer show phpmailer/phpmailer

    This should display information about the installed package, including the version number.

  4. To use PHPMailer in your PHP script, you'll need to require the autoloader file. You can do this by adding the following line at the top of your PHP script:

    require 'vendor/autoload.php';

    This will load the PHPMailer autoloader, which will allow you to use the PHPMailer classes.

  5. Now you can use PHPMailer in your script by creating an instance of the PHPMailer class and setting up the mail settings:

    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

$mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'your_password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;

$mail->setFrom('[email protected]', 'Your Name'); $mail->addAddress('[email protected]', 'Recipient Name'); $mail->Subject = 'Test Email'; $mail->Body = 'This is a test email';

if (!$mail->send()) { echo 'Error: '. $mail->ErrorInfo; } else { echo 'Email sent!'; }


Make sure to replace the placeholders with your actual email settings and recipient information.

That's it! With these steps, you should be able to install and use PHPMailer with Composer.