承接 originphp/email 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

originphp/email

Composer 安装命令:

composer require originphp/email

包简介

OriginPHP Email

README 文档

README

license build coverage

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

Installation

To install from the command line type

$ composer require originphp/email

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

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)

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)

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('<p>This is the html content</p>')
    ->format('both')
    ->send();

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('<p>This is the html content</p>')
    ->format('html')
    ->send();

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

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

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

To generate Oauth2 tokens, you can use the 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

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 https://developers.google.com/gmail/api/quickstart/php 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
  1. 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.

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

originphp/email 适用场景与选型建议

originphp/email 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.14k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mail」 「email」 「smtp」 「originPHP」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 originphp/email 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 originphp/email 我们能提供哪些服务?
定制开发 / 二次开发

基于 originphp/email 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-11