Privacy & Policy

ads

pop ads

Sending Emails in PHP with PHPMailer


In this article, we’ll talk about why you should use PHPMailer instead of PHP’s mail() function, and we’ll show some code samples on how to use this library and build a side project called #contact form which will be able in sending message directly from localhost using php.

What is PHPMailer?

PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming.

It was first released way back in 2001, and since then it has become a PHP developer’s favorite way of sending emails programmatically, aside from a few other fan favorites like Swiftmailer.


Would PHPMailer replace PHP mail() function?

In most cases, it’s an alternative to PHP’s mail() function, but there are many other cases where the mail() function is simply not flexible enough to achieve what you need. The mail() function allows you to send emails directly from a script.

However, PHPMailer provides an object-oriented architecture, whereas mail() is not object oriented. PHP developers generally hate to create $headers strings while sending emails using the mail() function because they require a lot of escaping. PHPMailer makes this a breeze. Developers also need to write dirty code (escaping characters, encoding and formatting) to send attachments and HTML based emails when using the mail() function, whereas PHPMailer makes this painless and easy for developers to use. 

But inmost cases, this two functions have their advantages and disadvantages.

Advantages to using PHP's built-in mail function, no external library/wrapper:
  • You don't need anything outside of PHP.
  • You don't need to learn a new API.
  • You don't have to worry about a PHP upgrade or such breaking the script.
  • You don't have to worry about an updated version not working on your PHP installation.
  • No need to learn OOP when using the mail function.


Disdvantages to using PHP's built-in mail function, no external library/wrapper
  • wrong format of mail header or content (e.g. differences in line break between Windows/Unix)
  • sendmail not installed or configured on your server (php.ini)
  • the mail provider of the recipeint does not allow mails send by PHP mail(); common spam protection.
  • This function opens and closes an SMTP socket for each email, which is not very efficient.
  • Errors in the format of header or content can cause that mails are treated as SPAM which would enable message sent to gmail lands inside the spam folder..


Advantages to using PHPMailer.

  • In case, if it is not possible to send an email, PHPMailer will print error messages in more than 40 languages.
  • PHPMailer makes it easy to change any parameter dynamically right in your PHP script.
  • SMTP protocol support and authentication are integrated over SSL and TLS.
  • It can send an alternative plaintext version of email for email clients who don’t have HTML.
  • Integrated SMTP support - send without a local mail server
  • Send emails with multiple To, CC, BCC and Reply-to addresses
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Add attachments, including inline
  • Support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
  • SMTP authentication with LOGIN, PLAIN, CRAM-MD5 and XOAUTH2 mechanisms over SSL and SMTP+STARTTLS transports
  • Validates email addresses automatically.

KK, enough talking now let partialize all what weve said.



Installing PHPMailer

You can install PHPMailer using Composer by running this command in the terminal:

composer require phpmailer/phpmailer

Note: for this command to run, you must have the composer.exe installed on your local machine. for installation guide, visit Composer Download Guide


Sending Email from a Local Web Server Using PHPMailer.

STEP 1) Create a file called index.php, style.csssendmail.php.

The first file would contain our UI i.e html tags, the second file would contain our styling for the html tags and last file would contain the logic in sending the mail.

STEP 2) Copy the code below and paste it inside your index.php &style.css file.

index.php file


style.css



After adding the following code above in their respective directory, preview the index.php file in browser. I make use of xampp localserver and whatever php code you are writting must be placed inside the htdocs folder, if you went through this process successfully, open your browser and type in the parth below:  

localhost/your-folder-name  in my own case it is localhost/Send Mail . if you've done the correct thing, it should look like the image below.



and finally, sendmail.php file


 

As you can see from the followng code above, we added some logic to our appliocation:

  1. Creating a conditional statement that checks if a post request is made to that page and not by a get request, this would prevent the user from entering that page without clicking on the send mail button.
  2. We also sanitized the user inputs by using the PHP htmlspecialchar()  function which would remove any html characters if the user try puttin g them in the input fields. 
  3. We also validated the user gmail address by use the PHP filter_var() function which  filters a single variable with a specified filter. It takes two pieces of data: The variable you want to check & The type of check to use.
  4. We also use the http header() redirect function which redirect the user if the condition are eithir met or not.
  5. We also use the PHPMailer methods in sending our message to the specified gmail address given.
Trying to send an message tomy gmail account:






And that it guys, Building A Contact Form Using PHP and PHPMailer. For source code -- visit Sendmails with PHPMailer

usefull links

--PHPMailer Docs: PHPMailer Docs

--Composer Download: Composer Download

Conclusion

If you’re a PHP developer, there’s little chance of avoiding having to send emails programmatically. While you may opt for third-party services like SendGrid, sometimes that just isn’t an option, and rolling your own email sending library even less so. That’s where PHPMailer and its alternatives (Zend Mail, Swift Mailer, and so on) come in.
If you enjoy reading through this article, consider sharing it and help others get to know more about this. :)


Post a Comment

0 Comments