PHPackages                             originphp/email - 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. originphp/email

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

originphp/email
===============

OriginPHP Email

2.1.0(5y ago)114.1k↓68.8%4MITPHPPHP &gt;=7.3.0

Since Oct 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/originphp/email)[ Packagist](https://packagist.org/packages/originphp/email)[ Docs](https://www.originphp.com)[ RSS](/packages/originphp-email/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (5)Versions (12)Used By (4)

Email
=====

[](#email)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/originphp/email/workflows/CI/badge.svg)](https://github.com/originphp/email/actions)[![coverage](https://camo.githubusercontent.com/792254d154899b1ca2dd41826aa60e8ef7ace02352030ec5ced808bbc427542e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f726967696e7068702f656d61696c2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/originphp/email?branch=master)

The `Email` class enables you to send emails easily through SMTP.

Installation
------------

[](#installation)

To install from the command line type

```
$ composer require originphp/email

```

Email Configuration
-------------------

[](#email-configuration)

In your bootstrap or application config. If you create a default account, then you do not need to specify an account or configure the instance of the email.

```
Email::config('default',[
    'host' => 'smtp.example.com',
    'port' => 465,
    'username' => 'demo@example.com',
    'password' => 'secret',
    'timeout' => 5,
    'ssl' => true,
    'tls' => false
]);
```

The keys for the config are as follows:

- *host*: this is smtp server hostname
- *port*: port number default 25
- *username*: the username to access this SMTP server
- *password*: the password to access this SMTP server
- *ssl*: default is false, set to true if you want to connect via SSL
- *tls*: default is false, set to true if you want to enable TLS
- *timeout*: how many seconds to timeout
- *domain*: When we send the HELO command to the sever we have to identify your hostname, so we will use localhost or HTTP\_SERVER var if client is not set.

You can also pass an array with configuration when you create an instance of the Email object.

```
$config = [
    'host' => 'smtp.example.com',
    'port' => 25,
    'username' => 'demo@example.com',
    'password' => 'secret',
    'timeout' => 5,
    'ssl' => true,
    'tls' => false
]
$email = new Email($config);
```

You can also pass keys such as `from`,`to`,`cc`,`bcc`,`sender` and `replyTo` this pass the data to its functions either as string if its just an email or an array if you want to include a name. Remember if you are going to automatically cc or bcc somewhere, then you have to next call addBcc or addCc to ensure that you don't overwrite this.

For example

```
[
    'from' => ['james@originphp.com' => 'James'],
    'replyTo' => 'no-reply@originphp.com'
    'bcc' => ['someone@origin.php' => 'Someone','another-person@example.com']
]
```

Sending Emails
--------------

[](#sending-emails)

The default email sending behavior is to send a text version. However it best practice to send both HTML and text and this reduces the risk of your email ending up in spam folders.

When an email is sent it will return a Message object, if an error is encountered when sending then the email class will throw an exception which you can catch in try/catch block.

### Sending an Email (Text)

[](#sending-an-email-text)

To send an email

```
use Origin\Email\Email;
$Email = new Email();
$Email->to('somebody@originphp.com')
    ->from('me@originphp.com')
    ->subject('This is a test')
    ->textMessage('This is the text content')
    ->send();
```

### Send both a HTML and Text Version (Recommend)

[](#send-both-a-html-and-text-version-recommend)

To send an email with both HTML and text versions:

```
use Origin\Email\Email;
$Email = new Email();
$Email->to('somebody@originphp.com')
    ->from('me@originphp.com')
    ->subject('This is a test')
    ->textMessage('This is the text content')
    ->htmlMessage('This is the html content')
    ->format('both')
    ->send();
```

### Sending HTML Only Email

[](#sending-html-only-email)

To send a HTML only email, you need to tell the Email utility use the HTML format.

```
use Origin\Email\Email;
$Email = new Email());
$Email->to('somebody@originphp.com')
    ->from('me@originphp.com')
    ->subject('This is a test')
    ->htmlMessage('This is the html content')
    ->format('html')
    ->send();
```

Adding Attachments
------------------

[](#adding-attachments)

To add attachments to an email message

```
use Origin\Email\Email;
$Email = new Email();
$Email->to('somebody@originphp.com')
    ->from('me@originphp.com')
    ->subject('This is a test')
    ->textMessage('This is the text content')
    ->addAttachment($filename1)
    ->addAttachment($filename2,'Logo.png')
    ->send();
```

Using Multiple Accounts
-----------------------

[](#using-multiple-accounts)

If you have configured using `Email::config('gmail',$config)` then you can use it like this

```
use Origin\Email\Email;
$Email = new Email('gmail');
```

Or

```
use Origin\Email\Email;
$Email = new Email();
$Email->to('somebody@originphp.com')
    ->from('me@originphp.com')
    ->subject('This is a test')
    ->textMessage('This is the text content')
    ->account('gmail')
    ->send();
```

Oauth2
------

[](#oauth2)

To configure your email account to use Oauth2 authentication, instead of providing a password you can use a token.

```
 Email::config('gsuite', [
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'username' => 'somebody@gmail.com',
    'token' => 'b1816172fd2ba98f3af520ef572e3a47', // see token generation below
    'ssl' => false,
    'tls' => true
]);
```

### Generating Tokens

[](#generating-tokens)

To generate Oauth2 tokens, you can use the [thephpleague/oauth2-client](https://github.com/thephpleague/oauth2-client) package or if you are using Google (Gsuite/Gmail) then you can use the command line script provided. The script provided is only ideal for sending emails from your own account, rather than from a user account.

#### Google Command-Line OAuth Token Generator

[](#google-command-line-oauth-token-generator)

The Google Client Library API allows you to generate tokens from the command line (without having to redirect to a script), and I have included a quick script for this.

To obtain an Oauth2 token that you can use with your Gsuite/Gmail account follow these instructions.

1. Enable the Gsuite API for your email account by going to  and then click on `Enable the Gmail API` button, click on `Desktop App` then the `Download Client Configuration` button. Once you have done this, save the data file to `data/credentials.json` in the `vendor/originphp/email/` folder.
2. Install Google Client Library (PHP), by running the following command:

```
$ composer require google/apiclient:^2.0

```

3. Run the Google CLI script.

```
$ vendor/bin/google

```

Now copy the URL into your browser and follow the instructions on screen, and this will provide you with a code. Paste the code into your console window, and your token will be displayed on the screen. The token JSON will be saved to `data/token.json` for future reference.

4. Add the token that was generated to your email configuration.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~54 days

Recently: every ~65 days

Total

11

Last Release

1858d ago

Major Versions

1.2.5 → 2.0.02021-01-04

PHP version history (3 changes)1.0.0PHP ^7.2.0

1.2.5PHP &gt;=7.2.0

2.0.0PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e8a821333d9c7b7bc2ad3d164d142f65cd3912dea78033d31f76b0f19ba8a0c?d=identicon)[originphp](/maintainers/originphp)

---

Top Contributors

[![jamielsharief](https://avatars.githubusercontent.com/u/20553479?v=4)](https://github.com/jamielsharief "jamielsharief (68 commits)")

---

Tags

mailemailsmtporiginPHP

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/originphp-email/health.svg)

```
[![Health](https://phpackages.com/badges/originphp-email/health.svg)](https://phpackages.com/packages/originphp-email)
```

###  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)
