PHPackages                             dimarick/net\_smtp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. dimarick/net\_smtp

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

dimarick/net\_smtp
==================

An implementation of the SMTP protocol

v2.0.0beta4(6y ago)013.7k1BSD-2-ClausePHPPHP &gt;=5.6.0

Since Aug 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/dimarick/Net_SMTP)[ Packagist](https://packagist.org/packages/dimarick/net_smtp)[ Docs](http://pear.github.io/Net_SMTP/)[ RSS](/packages/dimarick-net-smtp/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (12)Used By (0)

The Net\_SMTP Package
=====================

[](#the-net_smtp-package)

User Documentation
------------------

[](#user-documentation)

Author:Jon PariseContact:Table of Contents

- [1 Dependencies](#dependencies)
    - [1.1 The `PEAR_Error` Class](#the-pear-error-class)
    - [1.2 The `Net_Socket` Package](#the-net-socket-package)
    - [1.3 The `Auth_SASL` Package](#the-auth-sasl-package)
- [2 Error Handling](#error-handling)
- [3 SMTP Authentication](#smtp-authentication)
    - [3.1 DIGEST-MD5](#digest-md5)
    - [3.2 CRAM-MD5](#cram-md5)
    - [3.3 LOGIN](#login)
    - [3.4 PLAIN](#plain)
- [4 Secure Connections](#secure-connections)
- [5 Sending Data](#sending-data)
- [6 Data Quoting](#data-quoting)
- [7 Server Responses](#server-responses)
- [8 Debugging](#debugging)
- [9 Examples](#examples)
    - [9.1 Basic Use](#basic-use)

[1 Dependencies](#id1)
----------------------

[](#1dependencies)

### [1.1 The `PEAR_Error` Class](#id2)

[](#11the-pear_error-class)

The Net\_SMTP package uses the [PEAR\_Error](http://pear.php.net/manual/en/core.pear.pear-error.php) class for all of its [error handling](#error-handling).

### [1.2 The `Net_Socket` Package](#id3)

[](#12the-net_socket-package)

The [Net\_Socket](http://pear.php.net/package/Net_Socket) package is used as the basis for all network communications. Connection options can be specified via the $socket\_options construction parameter:

```
$socket_options = array('ssl' => array('verify_peer_name' => false));
$smtp = new Net_SMTP($host, null, null, false, 0, $socket_options);
```

**Note:** PHP 5.6 introduced [OpenSSL changes](http://php.net/manual/en/migration56.openssl.php). Peer certificate verification is now enabled by default. Although not recommended, $socket\_options can be used to disable peer verification (as shown above).

### [1.3 The `Auth_SASL` Package](#id4)

[](#13the-auth_sasl-package)

The [Auth\_SASL](http://pear.php.net/package/Auth_SASL) package is an optional dependency. If it is available, the Net\_SMTP package will be able to support the [DIGEST-MD5](#digest-md5) and [CRAM-MD5](#cram-md5) SMTP authentication methods. Otherwise, only the [LOGIN](#login) and [PLAIN](#plain) methods will be available.

[2 Error Handling](#id5)
------------------------

[](#2error-handling)

All of the Net\_SMTP class's public methods return a [PEAR\_Error](http://pear.php.net/manual/en/core.pear.pear-error.php) object if an error occurs. The standard way to check for a PEAR\_Error object is by using [PEAR::isError()](http://pear.php.net/manual/en/core.pear.pear.iserror.php):

```
if (PEAR::isError($error = $smtp->connect())) {
    die($error->getMessage());
}
```

[3 SMTP Authentication](#id6)
-----------------------------

[](#3smtp-authentication)

The Net\_SMTP package supports the SMTP authentication standard (as defined by [RFC-2554](http://www.ietf.org/rfc/rfc2554.txt)). The Net\_SMTP package supports the following authentication methods, in order of preference:

### [3.1 DIGEST-MD5](#id7)

[](#31digest-md5)

The DIGEST-MD5 authentication method uses [RSA Data Security Inc.](http://www.rsasecurity.com/)'s MD5 Message Digest algorithm. It is considered the most secure method of SMTP authentication.

**Note:** The DIGEST-MD5 authentication method is only supported if the [AUTH\_SASL](http://pear.php.net/package/Auth_SASL) package is available.

### [3.2 CRAM-MD5](#id8)

[](#32cram-md5)

The CRAM-MD5 authentication method has been superseded by the [DIGEST-MD5](#digest-md5)method in terms of security. It is provided here for compatibility with older SMTP servers that may not support the newer DIGEST-MD5 algorithm.

**Note:** The CRAM-MD5 authentication method is only supported if the [AUTH\_SASL](http://pear.php.net/package/Auth_SASL) package is available.

### [3.3 LOGIN](#id9)

[](#33login)

The LOGIN authentication method encrypts the user's password using the [Base64](http://www.php.net/manual/en/function.base64-encode.php) encoding scheme. Because decrypting a Base64-encoded string is trivial, LOGIN is not considered a secure authentication method and should be avoided.

### [3.4 PLAIN](#id10)

[](#34plain)

The PLAIN authentication method sends the user's password in plain text. This method of authentication is not secure and should be avoided.

[4 Secure Connections](#id11)
-----------------------------

[](#4secure-connections)

If [secure socket transports](http://www.php.net/transports) have been enabled in PHP, it is possible to establish a secure connection to the remote SMTP server:

```
$smtp = new Net_SMTP('ssl://mail.example.com', 465);
```

This example connects to `mail.example.com` on port 465 (a common SMTPS port) using the `ssl://` transport.

[5 Sending Data](#id12)
-----------------------

[](#5sending-data)

Message data is sent using the `data()` method. The data can be supplied as a single string or as an open file resource.

If a string is provided, it is passed through the [data quoting](#data-quoting) system and sent to the socket connection as a single block. These operations are all memory-based, so sending large messages may result in high memory usage.

If an open file resource is provided, the `data()` method will read the message data from the file line-by-line. Each chunk will be quoted and sent to the socket connection individually, reducing the overall memory overhead of this data sending operation.

Header data can be specified separately from message body data by passing it as the optional second parameter to `data()`. This is especially useful when an open file resource is being used to supply message data because it allows header fields (like *Subject:*) to be built dynamically at runtime.

```
$smtp->data($fp, "Subject: My Subject");
```

[6 Data Quoting](#id13)
-----------------------

[](#6data-quoting)

By default, all outbound string data is quoted in accordance with SMTP standards. This means that all native Unix (`\n`) and Mac (`\r`) line endings are converted to Internet-standard CRLF (`\r\n`) line endings. Also, because the SMTP protocol uses a single leading period (`.`) to signal an end to the message data, single leading periods in the original data string are "doubled" (e.g. "`..`").

These string transformation can be expensive when large blocks of data are involved. For example, the Net\_SMTP package is not aware of MIME parts (it just sees the MIME message as one big string of characters), so it is not able to skip non-text attachments when searching for characters that may need to be quoted.

Because of this, it is possible to extend the Net\_SMTP class in order to implement your own custom quoting routine. Just create a new class based on the Net\_SMTP class and reimplement the `quotedata()` method:

```
require 'Net_SMTP.php';

class Net_SMTP_custom extends Net_SMTP
{
    function quotedata($data)
    {
        /* Perform custom data quoting */
    }
}
```

Note that the `$data` parameter will be passed to the `quotedata()`function [by reference](http://www.php.net/manual/en/language.references.pass.php). This means that you can operate directly on `$data`. It also the overhead of copying a large `$data` string to and from the `quotedata()` method.

[7 Server Responses](#id14)
---------------------------

[](#7server-responses)

The Net\_SMTP package retains the server's last response for further inspection. The `getResponse()` method returns a 2-tuple (two element array) containing the server's response code as an integer and the response's arguments as a string.

Upon a successful connection, the server's greeting string is available via the `getGreeting()` method.

[8 Debugging](#id15)
--------------------

[](#8debugging)

The Net\_SMTP package contains built-in debugging output routines (disabled by default). Debugging output must be explicitly enabled via the `setDebug()`method:

```
$smtp->setDebug(true);
```

The debugging messages will be sent to the standard output stream by default. If you need more control over the output, you can optionally install your own debug handler.

```
function debugHandler($smtp, $message)
{
    echo "[$smtp->host] $message\n";
}

$smtp->setDebug(true, "debugHandler");
```

[9 Examples](#id16)
-------------------

[](#9examples)

### [9.1 Basic Use](#id17)

[](#91basic-use)

The following script demonstrates how a simple email message can be sent using the Net\_SMTP package:

```
require 'Net/SMTP.php';

$host = 'mail.example.com';
$from = 'user@example.com';
$rcpt = array('recipient1@example.com', 'recipient2@example.com');
$subj = "Subject: Test Message\n";
$body = "Body Line 1\nBody Line 2";

/* Create a new Net_SMTP object. */
if (! ($smtp = new Net_SMTP($host))) {
    die("Unable to instantiate Net_SMTP object\n");
}

/* Connect to the SMTP server. */
if (PEAR::isError($e = $smtp->connect())) {
    die($e->getMessage() . "\n");
}

/* Send the 'MAIL FROM:' SMTP command. */
if (PEAR::isError($smtp->mailFrom($from))) {
    die("Unable to set sender to \n");
}

/* Address the message to each of the recipients. */
foreach ($rcpt as $to) {
    if (PEAR::isError($res = $smtp->rcptTo($to))) {
        die("Unable to add recipient : " . $res->getMessage() . "\n");
    }
}

/* Set the body of the message. */
if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) {
    die("Unable to send data\n");
}

/* Disconnect from the SMTP server. */
$smtp->disconnect();
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~192 days

Recently: every ~299 days

Total

10

Last Release

2210d ago

Major Versions

1.7.3 → v2.0.0beta12017-09-28

PHP version history (3 changes)1.6.3PHP &gt;=4.0.5

1.7.0PHP &gt;=5.4.0

v2.0.0beta1PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e32ac47d3425e1d7bd00bfc05c95ad7083ffb9ed12dd7813431cbf7f7a385e0b?d=identicon)[dimarick](/maintainers/dimarick)

---

Top Contributors

[![jparise](https://avatars.githubusercontent.com/u/10311?v=4)](https://github.com/jparise "jparise (183 commits)")[![CloCkWeRX](https://avatars.githubusercontent.com/u/365751?v=4)](https://github.com/CloCkWeRX "CloCkWeRX (8 commits)")[![alecpl](https://avatars.githubusercontent.com/u/546788?v=4)](https://github.com/alecpl "alecpl (5 commits)")[![dimarick](https://avatars.githubusercontent.com/u/2605934?v=4)](https://github.com/dimarick "dimarick (4 commits)")[![ashnazg](https://avatars.githubusercontent.com/u/100170?v=4)](https://github.com/ashnazg "ashnazg (4 commits)")[![till](https://avatars.githubusercontent.com/u/27003?v=4)](https://github.com/till "till (2 commits)")[![dsoares](https://avatars.githubusercontent.com/u/673736?v=4)](https://github.com/dsoares "dsoares (1 commits)")[![eehakkin](https://avatars.githubusercontent.com/u/2233088?v=4)](https://github.com/eehakkin "eehakkin (1 commits)")[![haoqis](https://avatars.githubusercontent.com/u/1284986?v=4)](https://github.com/haoqis "haoqis (1 commits)")[![derickr](https://avatars.githubusercontent.com/u/208074?v=4)](https://github.com/derickr "derickr (1 commits)")[![legoktm](https://avatars.githubusercontent.com/u/81392?v=4)](https://github.com/legoktm "legoktm (1 commits)")[![mj](https://avatars.githubusercontent.com/u/5277?v=4)](https://github.com/mj "mj (1 commits)")[![ostin654](https://avatars.githubusercontent.com/u/2156040?v=4)](https://github.com/ostin654 "ostin654 (1 commits)")[![roelvanmeer](https://avatars.githubusercontent.com/u/11042467?v=4)](https://github.com/roelvanmeer "roelvanmeer (1 commits)")[![stigsb](https://avatars.githubusercontent.com/u/169939?v=4)](https://github.com/stigsb "stigsb (1 commits)")[![convissor](https://avatars.githubusercontent.com/u/187163?v=4)](https://github.com/convissor "convissor (1 commits)")

---

Tags

mailemailsmtp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dimarick-net-smtp/health.svg)

```
[![Health](https://phpackages.com/badges/dimarick-net-smtp/health.svg)](https://phpackages.com/packages/dimarick-net-smtp)
```

###  Alternatives

[mlocati/spf-lib

Parse, build and validate SPF (Sender Policy Framework) DNS records

67867.9k2](/packages/mlocati-spf-lib)[pear/net_smtp

An implementation of the SMTP protocol

263.0M16](/packages/pear-net-smtp)[thefox/smtpd

SMTP server (library) written in pure PHP.

1302.4k1](/packages/thefox-smtpd)[ikkez/f3-mailer

SMTP plugin wrapper for PHP Fat-Free Framework

198.7k2](/packages/ikkez-f3-mailer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
