laswitchtech/php-smtp

This PHP SMTP email library provides a simple and easy-to-use solution for sending emails using SMTP servers.

v2.1.13 2024-05-02 12:21 UTC

README

GitHub repo logo

phpSMTP

License GitHub repo size GitHub top language Version

Description

This library is a PHP implementation of the Simple Mail Transfer Protocol (SMTP) that allows sending emails using an SMTP server. The library provides a class called phpSMTP that encapsulates the functionality of establishing a connection with an SMTP server, authenticating the user, setting up the email message, and sending the email.

The class provides several methods to configure the email message, such as setting the sender, recipients, subject, body, and attachments. It also provides a method to load an email template and insert the message body into it.

Additionally, the class allows for debugging the SMTP session by logging the communication between the client and the server to a file. It also provides error handling and logging mechanisms to help identify and troubleshoot any issues that might occur during the sending process.

Features

  • Send emails using SMTP protocol
  • Supports TLS and SSL encryption
  • Authenticate SMTP connections with username and password
  • Support for HTML and plain text messages
  • Support for email attachments
  • Customizable email templates using placeholders
  • Option to send to multiple recipients
  • Option to add CC and BCC recipients
  • Option to specify a reply-to email address
  • Automatic email body conversion from HTML to plain text
  • Debug mode for troubleshooting SMTP connections
  • Log events to file
  • Composer package for easy installation and integration with other PHP projects

Why you might need it?

This PHP SMTP email library provides a simple and easy-to-use solution for sending emails using SMTP servers. It offers a range of features such as support for multiple recipients, attachments, and HTML emails. With this library, you can easily integrate email functionality into your PHP applications and streamline your email communication. It also includes error handling and logging to ensure that any issues are identified and resolved quickly. Overall, this library is a valuable tool for any PHP developer looking to incorporate email functionality into their projects.

Can I use this?

Sure!

License

This software is distributed under the GNU General Public License v3.0 license. Please read LICENSE for information on the software availability and distribution.

Requirements

PHP >= 5.5.0

Security

Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.

Installation

Using Composer:

composer require laswitchtech/php-smtp

How do I use it?

Examples

Connecting to a SMTP Server

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Connect to your server
$phpSMTP->connect("username@domain.com","*******************","mail.domain.com","465","ssl");

Authenticating a user against a SMTP server

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

if($phpSMTP->login("username@domain.com","*******************","mail.domain.com","465","ssl")){
  echo "User Authenticated!\n";
}

Sending an email

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Connect to your server
$phpSMTP->connect("username@domain.com","*******************","mail.domain.com","465","ssl");

if($phpSMTP->send([
  "to" => "username@domain.com",
  "subject" => "Lorem",
  "body" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
])){
  echo "Message Sent!\n";
}

Creating a template

You can create templates in HTML or PLAIN TEXT.

Here is a list of placeholders that can be used:

  • %FROM%
  • %REPLY-TO%
  • %TO% (first in the list)
  • %CC% (first in the list)
  • %BCC% (first in the list)
  • %SUBJECT%
  • %BODY%

Example in plain text:

phpSMTP
============================================================================================================

%SUBJECT%
------------------------------------------------------------------------------------------------------------
Hi %TO%,
%BODY%

Sincerely,
phpSMTP

Adding a template

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Add your template
$phpSMTP->addTemplate('html','tmp/templates/default.html');

Selecting a template

//Import SMTP class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\SMTP\phpSMTP;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance of phpSMTP
$phpSMTP = new phpSMTP();

//Select your template
$phpSMTP->setTemplate('html');