Search code examples
phpemailsmtpgmail

Can I use gmail as smtp server for my website


Hello I am trying to get a website up and running. It is currently hosted on AWS, so I do not have my own smtp server running at this moment. So after reading a few articles, I have understood that we could used gmail as a smtp server.

I wanted to double check if what I read was right, I am going to use smart job board software, can I plug in the values provided by gmail and use that as an smtp server??


Solution

  • Yes, Google allows connections through their SMTP and allows you to send emails from your GMail account.

    There are a lot of PHP mail scripts that you can use. Some of the most popular SMTP senders are: PHPMailer (with an useful tutorial) and SWIFTMailer (and their tutorial).

    The data you need to connect and send emails from their servers are your GMail account, your password, their SMTP server (in this case smtp.gmail.com) and port (in this case 465) also you have to make sure that emails are being sent over SSL.

    A quick example of sending an email like that with PHPMailer:

    <?php
        require("class.phpmailer.php");
    
        $mail = new PHPMailer();
    
        $mail->IsSMTP();  // telling the class to use SMTP
        $mail->SMTPAuth   = true; // SMTP authentication
        $mail->Host       = "smtp.gmail.com"; // SMTP server
        $mail->Port       = 465; // SMTP Port
        $mail->Username   = "[email protected]"; // SMTP account username
        $mail->Password   = "your.password";        // SMTP account password
    
        $mail->SetFrom('[email protected]', 'John Doe'); // FROM
        $mail->AddReplyTo('[email protected]', 'John Doe'); // Reply TO
    
        $mail->AddAddress('[email protected]', 'Jane Doe'); // recipient email
    
        $mail->Subject    = "First SMTP Message"; // email subject
        $mail->Body       = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";
    
        if(!$mail->Send()) {
          echo 'Message was not sent.';
          echo 'Mailer error: ' . $mail->ErrorInfo;
        } else {
          echo 'Message has been sent.';
        }
    ?>