Send Emails with PHP & Gmail

Php

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 https://howcode.org for more!

Link to DigitalOcean: http://howco.de/d_ocean

Link to howCode Facebook: http://howco.de/fb
Link to howCode Twitter: http://howco.de/twitter
Link to /r/howCode: http://howco.de/reddit

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.
    <?php

    require_once('PHPMailer/PHPMailerAutoload.php');
    $mail = new PHPMailer();
    $mail-> SMTPDebug= 2;
    $mail-> isSMTP();
    $mail-> SMTPAuth = true;
    $mail-> username = 'darshgondalia10@gmail.com';
    $mail-> password = 'my password';
    $mail-> SMTPSecure('ssl');
    $mail-> HOST = 'smtp.gmail.com';
    $mail-> PORT = '465';
    $mail-> isHTML(true);

    $mail-> setFrom = 'darshgondalia10@gmail.com';
    $mail-> subject= 'WEBSITE FEEDBACK';
    $mail-> body = 'hi';
    $mail-> addAddress = 'darshgondalia10@gmail.com';
    $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 https://github.com/PHPMailer/PHPMailer/archive/v5.2.25.zip and this is the code he wrote:

    <?php
    require_once('PHPMailerAutoload.php');

    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->SMTPAuth = TRUE;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '465';
    $mail->isHTML();
    $mail->Username = 'YOURGMAILACCOUNT@gmail.com';
    $mail->Password = 'YOURPASSWORD';
    $mail->SetFrom('no-reply@nocode.com');
    $mail->Subject = 'Hola Mundo';
    $mail->Body = 'Hola Mundo';
    $mail->AddAddress('DESTINYEMAIL@gmail.com');

    $mail->Send();

    ?>

  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. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
    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
    <?php
    //$mailto = "vuqarss@hotmail.com"
    // $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 = "smtp.live.com";
    $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");

    if(!$mail->Send())
    {
    echo "Mail Not Sent";
    }
    else
    {
    echo "Mail Sent";
    }
    ?>

  7. Never getting email

    <?php

    require_once('PHPMailer/PHPMailerAutoload.php');

    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->SMPTAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '465';
    $mail->isHTML();
    $mail->UserName = 'myemail@gmail.com';
    $mail->PassWord = '*******';
    $mail->setFrom('anotheremail@gmail.com');
    $mail->Subject = 'asdasdadas';
    $mail->Body = 'dgfdfgdfg';
    $mail->AddAddress(anotheremail@gmail.com');

    $mail->Send();

    ?>

  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 *