How to send email from PHP without SMTP server installed?

Ted James
1 min readApr 19, 2024

I have a classic LAMP platform (Debian, Apache2, PHP5 and MySQL) on a dedicated server.

I heard PHPMailer can send email without having installed SMTP. Is PHPMailer the best choice for this?

Solution

Yes, PHPMailer is a very good choice.

For example, if you want, you can use the googles free SMTP server (it’s like sending from your gmail account.), or you can just skip the smtp part and send it as a typical mail() call, but with all the correct headers etc. It offers multipart e-mails, attachments.

Pretty easy to setup too.

<?php

$mail = new PHPMailer(true);

//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "your-gmail-account@gmail.com"; // GMAIL username
$mail->Password = "your-gmail-password"; // GMAIL password
}

//Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
//Something went bad
echo "Fail - " . $mail->ErrorInfo;
}

?>

Answered By — Mārtiņš Briedis

Answer Checked By — Mary Flores (FixIt Volunteer)

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

--

--

Ted James

The world is my office, and every destination is my inspiration