Send Emails with PHP & Gmail


Video is ready, Click Here to View ×

In this video we’ll be learning how to send emails using the PHPMailer class.

We’ll be using Gmail as our SMTP server, this means we don’t need to host our own email server and it means our emails are less likely to be categorised as SPAM by the recipients.

Go to for more!

Link to DigitalOcean:

Link to howCode Facebook:
Link to howCode Twitter:
Link to /r/howCode:

Don’t forget…

45 thoughts on “Send Emails with PHP & Gmail

  1. Can we set the address that we are sending the email to to a variable so that we can send emails to more than just the singular one defined in the code?

    I have a contact form and I've been wanting to be able to email users back once they hit submit.

  2. I get "SMTP connect() failed"
    My code is the same as yours except I'm using port 587 since 465 gives me the "Username & Password not accepted" error for some reason.
    Any thoughts?

  3. Hi there, this is the exact code that I have typed in could you please tell me what the error is.

    $mail = new PHPMailer();
    $mail-> SMTPDebug= 2;
    $mail-> isSMTP();
    $mail-> SMTPAuth = true;
    $mail-> username = '';
    $mail-> password = 'my password';
    $mail-> SMTPSecure('ssl');
    $mail-> HOST = '';
    $mail-> PORT = '465';
    $mail-> isHTML(true);

    $mail-> setFrom = '';
    $mail-> subject= 'WEBSITE FEEDBACK';
    $mail-> body = 'hi';
    $mail-> addAddress = '';
    $mail-> send();

    I want to check if the mail is sent from me to myself.

  4. With debug level set to 3 I'm getting this error:
    Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1
    OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

    My website is hosted on my own PC for the time being and I haven't set up a mail program on it, thinking I could just send everything to Google's SMTP. However, it looks like I need to generate a certificate locally to send but I've no idea how to do that. Or else override the certificate requirement but again, I haven't a bog's notion.

    (openssl is enabled on my server, FWIW)

    Anyone else resolved a similar issue?

  5. Great video. Thank you very much. For anyone else trying this remember to download this copy of PHPMailer and this is the code he wrote:


    $mail = new PHPMailer();
    $mail->SMTPAuth = TRUE;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = '';
    $mail->Port = '465';
    $mail->Username = '';
    $mail->Password = 'YOURPASSWORD';
    $mail->Subject = 'Hola Mundo';
    $mail->Body = 'Hola Mundo';



  6. i am getting this error (Fixed click More)

    2018-02-22 11:22:01 SMTP ERROR: Failed to connect to server: (0)
    SMTP connect() failed.
    Mail Not Sent
    EDIT: i fixed it here is source code you can compare with your's I used last version.Hope it ll help you
    //$mailto = ""
    // $mailSub = "Sub";
    //$mailMsg = "Yeah man";

    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerException;

    require './PHPMailer/src/Exception.php';
    require './PHPMailer/src/PHPMailer.php';
    require './PHPMailer/src/SMTP.php';

    //require 'PHPMailer-5.2.25/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail ->IsSmtp();
    $mail ->SMTPDebug = 1;
    $mail ->SMTPAuth = true;
    $mail ->SMTPSecure = 'tsl';
    $mail ->Host = "";
    $mail ->Port = "25"; // or 587
    $mail ->IsHTML(true);
    $mail ->Username = "your email";
    $mail ->Password = "yourpassword";
    $mail ->SetFrom("your email","His name");
    $mail ->Subject = "Sunbrf";
    $mail ->Body ="hgvfjhv";
    $mail ->AddAddress("email you want to send message");

    echo "Mail Not Sent";
    echo "Mail Sent";

  7. Never getting email



    $mail = new PHPMailer();
    $mail->SMPTAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = '';
    $mail->Port = '465';
    $mail->UserName = '';
    $mail->PassWord = '*******';
    $mail->Subject = 'asdasdadas';
    $mail->Body = 'dgfdfgdfg';



  8. Fatal error: Uncaught Error: Class 'PHPMailer' not found in D:xamphtdocssend mailsend_mail.php:3 Stack trace: #0 {main} thrown in D:xamphtdocssend mailsend_mail.php on line 3 can u plz tell me how to slve this error

Leave a Reply

Your email address will not be published. Required fields are marked *