PHP - send email example

  • Install composer if doesn't already exists
  • Install phpmailer library with command - composer require phpmailer/phpmailer
  • Copy the code from above and paste it into your index.php file
  • Execute created file with command - php index.php
    
        require 'vendor/autoload.php';

        use PHPMailer\PHPMailer\PHPMailer;

        $mail = new PHPMailer(true);

        try {
            $mail->setFrom('[email protected]', 'Superman');
            $mail->addAddress('[email protected]', 'Batman');
            $mail->Subject = 'Battle';
            $mail->Body = 'Your text here.';
            $mail->send();
        } catch (PHPMailer\PHPMailer\Exception $e) {
            echo $e->errorMessage();
        } catch (\Exception $e) {
            echo $e->getMessage();
        }