ozcankrks.devbankai
TR
All posts

Using and Integrating PHPMailer 2022

The mail() function used to be enough on old servers; today it isn't. Reliable email over SMTP with PHPMailer.

On older Linux servers, we could send email directly with the mail() function by passing the parameters we wanted. Over time, because of abuse and reliability issues, this feature has been removed from most servers. So to make the contact form on our site work, we need another approach. The most reliable and convenient one is to integrate the PHPMailer library into our system. That way we keep our credentials safe without sharing them with anyone, and we can change them whenever we want.

How do you integrate PHPMailer?

To integrate PHPMailer, first download it from the GitHub repository where its developers share it as open source: click for the PHPMailer GitHub link The repository has many files and folders and is updated and improved by volunteer developers every day. Either download it and put it in a folder inside your site folder, or pull it into your project from a CDN. To send or receive mail, first add the code below to pull the library into your own PHP page (e.g. "sendmail.php") and get it ready to use. The only thing to watch for right now is the file paths.

PHP
use PHPMailerPHPMailerPHPMailer;use PHPMailerPHPMailerException;require 'path/to/PHPMailer/src/Exception.php';require 'path/to/PHPMailer/src/PHPMailer.php';require 'path/to/PHPMailer/src/SMTP.php';

If you don't load the functions above, or load them incorrectly, the system won't work, so move carefully.

The code below is a sample for sending an email. Fill in the relevant parts with your own details to customize it.

PHP
// Import PHPMailer classes into the global namespace// These must be at the top of your script, not inside a functionuse PHPMailerPHPMailerPHPMailer;use PHPMailerPHPMailerSMTP;use PHPMailerPHPMailerException;// Load Composer's autoloaderrequire 'vendor/autoload.php';// Instantiation and passing `true` enables exceptions$mail = new PHPMailer(true);try {    //Server settings    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output    $mail->isSMTP();                                            // Send using SMTP    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication    $mail->Username   = 'user@example.com';                     // SMTP username    $mail->Password   = 'secret';                               // SMTP password    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above    //Recipients    $mail->setFrom('from@example.com', 'Mailer');    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient    $mail->addAddress('ellen@example.com');               // Name is optional    $mail->addReplyTo('info@example.com', 'Information');    $mail->addCC('cc@example.com');    $mail->addBCC('bcc@example.com');    // Attachments    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name    // Content    $mail->isHTML(true);                                  // Set email format to HTML    $mail->Subject = 'Here is the subject';    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';    $mail->send();

Setting the PHPMailer language and Gmail connection settings

Since PHPMailer is used worldwide in every language, it lets you set your language so that words from your region work without special-character errors. To use it, add the code below right after the line that loads the library on the page that sends mail with PHPMailer.

PHP
// To load the French version$mail->setLanguage('fr', '/optional/path/to/language/directory/');

This is the example they share on GitHub; change fr to tr to set it to Turkish.

If you want to use PHPMailer with Gmail, you need to turn on the third-party apps setting, otherwise Google won't let you use its mail system. You can fix the Gmail connection error with the links below.

https://myaccount.google.com/lesssecureapps

https://accounts.google.com/DisplayUnlockCaptcha

After granting permission through the links above, your connection should work. I hope this helped; feel free to reach out if you have any questions.

Comments 0

No comments yet. Be the first.

Leave a comment

Never published.

© 2026 Özcan Karakuş · This site is built with Next.js and runs on my own server.