Tag: php

  • Sending Emails from PHP using Amazon SES

    To create a PHP script to sent email using Amazon SES, create a folder

    mkdir ses-test
    cd ses-test
    

    Install phpMailer using composer

    composer require phpmailer/phpmailer
    

    Create a PHP file with following content

    SMTPDebug = 2;
        $mail->isSMTP();
        $mail->Host = 'email-smtp.us-east-1.amazonaws.com';  // YOUR SES END POINT
        $mail->SMTPAuth = true;
        $mail->Username = 'AKIAJSKJUHBOU2ARXVLQ';                 // SMTP Username
        $mail->Password = 'AjMmd3gRkmTyCY2QoCDkWmkGKA345zuhseS7gV0X6uz0';                           // SMTP Password
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
    
        //Recipients
        $mail->setFrom('info@YOUR_SES_VERIFIED_DOMAIN.com', 'YOUR_SES_VERIFIED_DOMAIN.com');
        $mail->addAddress('[email protected]', 'Your Name');     // If your SES is in Sandbox, you need to verify your email. 
        $mail->addReplyTo('info@YOUR_SES_VERIFIED_DOMAIN.com', 'YOUR_SES_VERIFIED_DOMAIN.com');
    
    
        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body in bold!';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    

    Now test it by running

    root@ok:~/ses-test# php 1.php
    2018-01-15 08:45:54 SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2762711029 fDkTVziQIcNhSCkxwSpA
    2018-01-15 08:45:54 CLIENT -> SERVER: EHLO ok
    2018-01-15 08:45:54 SERVER -> CLIENT: 250-email-smtp.amazonaws.com
                                          250-8BITMIME
                                          250-SIZE 10485760
                                          250-STARTTLS
                                          250-AUTH PLAIN LOGIN
                                          250 Ok
    2018-01-15 08:45:54 CLIENT -> SERVER: STARTTLS
    2018-01-15 08:45:54 SERVER -> CLIENT: 220 Ready to start TLS
    2018-01-15 08:45:54 CLIENT -> SERVER: EHLO ok
    2018-01-15 08:45:54 SERVER -> CLIENT: 250-email-smtp.amazonaws.com
                                          250-8BITMIME
                                          250-SIZE 10485760
                                          250-STARTTLS
                                          250-AUTH PLAIN LOGIN
                                          250 Ok
    2018-01-15 08:45:54 CLIENT -> SERVER: AUTH LOGIN
    2018-01-15 08:45:54 SERVER -> CLIENT: 334 VXNlcm5hbWU6
    2018-01-15 08:45:54 CLIENT -> SERVER: 
  • Enable PHP Error Reporting

    To enable error reporting in PHP script, add

    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    To avoid PHP scripts showing errors, add

    error_reporting(0);
    ini_set('display_errors', '0');

    See php

  • composer

    composer is PHP package manager, to install, run

    curl -sS https://getcomposer.org/installer |  php -- --install-dir=/usr/local/bin --filename=composer

    Install Packages

    Run this every time you change composer.json or pull newer copy from git.

    composer install

    Show Installed Packages

    composer show -i

    Update Software

    Only run this if you need to update software versions. Don’t run it in production. Only run when you are project maintainer and need everyone else use latest version of packages.

    composer update