How to Add PHPMailer for Linux and Windows Hosting
PHPMailer is an open-source library for the scripting language PHP that enables the capability for securely sending email via a web server. If you want to add PHPMailer either you are using a Linux or Windows hosting you can do so via your File Manager. Read through this article to learn how to do it.
What Can You Do With PHPMailer
With PHPMailer you can do the following tasks in PHP:
- Send HTML emails
- Send emails with attachments
- Send emails via Gmail
- Send emails using your SMTP Server
- Send emails to multiple recipients all at once
How to Add PHPMailer for Linux and Windows Hosting
To add PHPMailer to your Linux or Windows Hosting, follow the steps below.
- Search for and download PHPMailer. Make sure to get it from a reliable source.
Important: To use PHPMailer, you will need a domain name and web hosting because you will need to set up a working email account. If you want to test the script, then create two email accounts: a sender and a recipient.
Visit this linked guide to learn how to create a personal email account. - Access your File Manager.
- Scroll to public_html folder and locate your php.ini file.
- Inside the public_html folder, create a new folder. Name the new folder, for example, PHPMailer00. Then, open the new folder.
- Extract the downloaded PHPMailer zipped file.
- Add the necessary classes in your PHP script. Or you can just use the simple sample script below:
<?php require 'PHPMailer00/class.phpmailer.php'; // filepath to the PHPMailer class require 'PHPMailer00/class.smtp.php'; $mail = new PHPMailer(); $mail->SMTPDebug = 2; $mail->Mailer = "smtp"; $mail->Host = "localhost"; $mail->Port = 587; $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "[email protected]"; // SMTP username $mail->Password = "password123"; // SMTP password $Mail->Priority = 1; $mail->AddAddress("[email protected]","Name"); $mail->SetFrom("[email protected]", "Info"); $mail->AddReplyTo("[email protected]", "Info"); $mail->Subject = "Hello"; $mail->Body = "This is my message."; $mail->WordWrap = 50; if(!$mail->Send()) { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?>
Note: Make sure to put closing tags (?>) at the bottom. Create a new .php file (e.g. test01.php) and save it in the same folder as the extracted PHPMailer folder. Paste the sample script here and then click Save. - Replace the following values with your own:
File path the path of the file that will perform the action or function required for PHPMailer to send emails Host your email hosting server Username your hosting username Password corresponding login password for your hosting SetFrom the email address you want the recipient to see as the sender AddAddress the email address of the recipient AddReplyto the email address you want the recipient to reply to Subject the subject of your email Body the body or main content of your email As an alternative to the PHPMailer script, you can also use the simple PHPmail script below instead:
<?php $to = '[email protected]'; $subject = 'Email Test'; $message = 'Hello World'; $headers = 'From: [email protected]' . "\r\n" . 'CC: [email protected]'; 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); echo "Message has been sent."; ?>
- Test if the script works by checking it out on your browser. To do this, put the following url in your address bar:
yourdomainname.com/folder name/filename.php
- If everything is in order and the script works, you will see the following message:
Message has been sent.
Also, check your recipient email inbox. If you got an email from "[email protected]" and it has the message you put in the "body" tag ("This is your message."), then the script works.
Well done! You’ve successfully added PHPMailer and completed this guide. Please contact us know if you need any further assistance in setting up your PHPMailer, or if you have any questions.