Setting up PHPMailer to work with Gmail SMTP configuration

I’m having trouble getting my contact form to work with PHPMailer and my Gmail account. I keep getting an SSL socket transport error when trying to send emails.

I’ve tested different combinations like using SSL with port 465 and TLS with port 587, but nothing seems to work. The error message says something about the SSL socket transport not being found and asks if I forgot to enable it in PHP configuration.

Here’s my current setup:

$fullName = $_POST['fullName'];
$userEmail = $_POST['userEmail'];
$userMessage = $_POST['userMessage'];

$altText = 'Plain text version of message';

function sendContactEmail($recipientEmail, $htmlContent, $plainContent) {
    require_once('PHPMailer/PHPMailer.php');
    $mailer = new PHPMailer();
    $mailer->isSMTP();
    $mailer->Host = "smtp.gmail.com";
    $mailer->SMTPDebug = 2;
    $mailer->SMTPAuth = true;
    $mailer->SMTPSecure = "ssl";
    $mailer->Port = 465;
    $mailer->Username = '[email protected]';
    $mailer->Password = 'mypassword';
    $mailer->Priority = 1;
    $mailer->CharSet = 'UTF-8';
    $mailer->Encoding = '8bit';
    $mailer->Subject = 'Contact Form Submission';
    $mailer->setFrom('[email protected]', 'Contact Form');
    $mailer->addAddress($recipientEmail);
    $mailer->isHTML(true);
    $mailer->Body = "$fullName $userEmail $userMessage";
    $mailer->AltBody = $plainContent;
    
    if($mailer->send()) {
        return true;
    } else {
        return false;
    }
}

$recipient = '[email protected]';
$result = sendContactEmail($recipient, $fullName, $userEmail, $userMessage, $altText);

if($result) {
    echo "<h2>Email sent successfully</h2>";
} else {
    echo "<h2>Failed to send email</h2>";
}

What am I missing here? Is there a PHP configuration issue or something else I need to check?

check your server’s firewall settings too - some hosts block outbound smtp connections on ports 465/587 by default. had this happen on shared hosting where everything looked right but emails wouldnt send. contact your hosting provider to whitelist gmail’s smtp servers if needed.

The SSL socket transport error typically means your PHP installation doesn’t have the OpenSSL extension enabled. You can verify this by running php -m | grep openssl in your terminal or checking phpinfo() for OpenSSL support. I had this exact problem when migrating to a new server last year. The solution was enabling the OpenSSL extension in php.ini by uncommenting the line extension=openssl. After that, restart your web server. Also worth noting that Google has deprecated regular password authentication for third-party apps. You’ll need to generate an App Password from your Google Account settings under Security > 2-Step Verification > App passwords. Use this 16-character password instead of your regular Gmail password in the PHPMailer configuration. This solved authentication issues I was having even after fixing the SSL problem.

Looking at your code, there are several issues beyond just the SSL configuration. First, your function parameters don’t match what you’re passing - you’ve defined sendContactEmail($recipientEmail, $htmlContent, $plainContent) but you’re calling it with four parameters plus the alt text variable. This mismatch could cause unexpected behavior. Second, make sure you’re importing all necessary PHPMailer classes. You should include require_once('PHPMailer/SMTP.php') and require_once('PHPMailer/Exception.php') alongside the main PHPMailer class. I encountered similar issues when setting up automated notifications for my web application. Try switching to TLS with port 587 instead of SSL with 465, as Gmail’s SMTP servers tend to be more reliable with TLS. Change $mailer->SMTPSecure = "tls" and $mailer->Port = 587. Also consider adding error handling to see the actual SMTP error message by using echo $mailer->ErrorInfo when the send fails.