How to send Emails via SMTP using PHPMailer?

Send Emails via SMTP using PHPMailer

In the world of internet, emails are preferred as professional way of communication. Because of this user create their own custom emails to send and receive mails from clients and visitors. These custom emails allows user to create their own identity on the internet. 
Thus, service providers provide various features t the users so that they can securely send and receive emails. There are many articles related to custom emails on the blog at www.blog.redserverhost.com. Today, I'm sharing an article that illustrates the procedure to send emails via SMTP using PHPMailer
Before continuing with the procedure, let us know about PHPMailer. PHPMailer is an open-source PHP code library that allows user to securely and easily send emails via PHP code from web server. SMTP is the protocol that is used to send emails on web server. To send email via SMTP using PHPMailer first you need to install PHPMailer and then use it to send email via SMTP.

How to Install PHPMailer?

You can easily install PHPMailer through SSH. For this, you must have SSH access, cPanel username and password. To install PHPMailer, follow the steps.
  • Login to SSH with required credentials.
  • Get inside public_html by using cd command.
  • Then execute the below given command and hit Enter.
composer require phpmailer/phpmailer
You have successfully installed PHPMailer.

How to send Emails using PHPMailer?

After installing PHPMailer, follow the below given steps to send emails using PHPMailer.
  • Login to server, i.e, cPanel.
  • Now, create a new mailer.php file inside public_html.
  • You need to edit this file. So, right click over the file and then Edit.
  • Copy the given code and paste it in the mailer.php file.
<?php require 'vendor/autoload.php'; use PHPMailer\PHPMailer\PHPMailer; $mail = new PHPMailer; $mail->isSMTP(); $mail->SMTPDebug = 2; $mail->Host = 'server81.redserverhost.com'; $mail->Port = 587; $mail->SMTPAuth = true; $mail->Username = 'admin@example.com'; $mail->Password = 'Enter_Email_Password'; $mail->setFrom('admin@example.com', 'Your Name'); $mail->addReplyTo('admin@example.com', 'Your Name'); $mail->addAddress('admin@new.com', 'Receiver Name'); $mail->Subject = 'Testing PHPMailer'; $mail->msgHTML(file_get_contents('message.html'), __DIR__); $mail->Body = 'This is a plain text message body'; //$mail->addAttachment('test.txt'); if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'The email message was sent.'; } ?>
  • Modify the code where required. You may refer to the marked lines in the code.
  • Click over Save Changes to save the code.
  • Visit the URL as domainname.com/mailer.php and hit the file from the outside. As the file gets hit, mail will be sent.
That's it !
Hope the article better explains the procedure to send emails via SMTP using PHPMailer. You can read more related articles on the blog. Share your feedback so that we bring more such articles for you.
Thanks for Reading !!
Web Hosting               Custom Emails             Cheap Web Hosting Services
Reseller Hosting           Cheap Reseller Hosting            VPS Hosting        

Comments