<commit_before>EmailSender Server
============

## Introduction

EmailSender Server 是一个基于 phpmailer 与 swoole 扩展的邮件服务，通过tcp方式连接进行异步发送邮件的服务。

Requirements
------------

**PHP5.4.0 or later**

Setup Database And Application
-----------

### Create database
```
CREATE DATABASE `mail` CHARACTER SET utf8 COLLATE utf8_general_ci; 
USE `mail`;
CREATE TABLE `mail` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `subject` varchar(255) NOT NULL,
  `body` text,
  `to` varchar(2000) NOT NULL,
  `cc` varchar(2000) NOT NULL,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '1: waiting, 2: pending, 3:complete',
  `ctime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'create time',
  `ftime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Finish time',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

### Setup application
```
cd yourproject/
composer install
# make a symbol link.
ln -s /usr/local/php-5.4.44/bin/php /usr/local/bin/php
# create database config file.
echo '[common]
database_type = mysql
port          = 3306
charset       = utf8
[local : common]
server        = 127.0.0.1
username      = root
password      = <PASSWORD>
database_name = mail

[test : common]
[product : common]' > application/config/database.ini
# create mail config file.
echo '[common]
server.log_file        = /tmp/output.log
server.max_request     = 1000
server.worker_num      = 2
server.task_worker_num = 2
server.user            = nobody
server.group           = nobody

phpmail.charset    = "utf-8"
phpmail.host       = "smtp.gmail.com"
phpmail.smtpauth   = 1
phpmail.username   = {your_username}
phpmail.password   = {<PASSWORD>}
phpmail.smtpsecure = tls
phpmail.port       = 587
phpmail.from       = "<EMAIL>"
phpmail.fromname   = "<NAME>"
phpmail.wordwrap   = 100

[local : common]
[test : common]
[product : common]' > application/config/mail.ini
```

Usage
-----------
### start server.
```
./server.sh start
```

### telnet

协议采用空格或者制表符做分隔，命令格式：CMD ARG1 ARG2 ARG3 ...
返回格式：CODE RESPONSE

注：若ARG存在空格，需要通过 rawurlencode

```
[lancer@Lancer-MacBookPro ~]$ telnet 127.0.0.1 8001
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
send subject emailbody <EMAIL>;<EMAIL> <EMAIL>
0 129
```

上例发送主题：subject，主体内容：emailbody，发送邮件：user1;user2，抄送：user3  
CODE=0表示成功，129表示Email Id，可以通过 `status 129` 查询邮件是否异步发送完成  
CODE>0表示失败，message 通过协议方式追加状态码后。

### use php in client

```php
class Util_Mail {

    public static function send($subject, $body, $to, $cc='') {
        $client = new swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
        $client->set(['open_eof_check' => true, 'package_eof' => "\r\n"]);
        if ( ! $client->connect('127.0.0.1', 8001))
            return false;<commit_msg>Update readme.<commit_after>EmailSender Server
============

## Introduction

EmailSender Server 是一个基于 phpmailer 与 swoole 扩展的邮件服务，通过tcp方式连接进行异步发送邮件的服务。

Requirements
------------

**PHP5.4.0 or later**

Setup Database And Application
-----------

### Create database
```
CREATE DATABASE `mail` CHARACTER SET utf8 COLLATE utf8_general_ci; 
USE `mail`;
CREATE TABLE `mail` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `subject` varchar(255) NOT NULL,
  `body` text,
  `to` varchar(2000) NOT NULL,
  `cc` varchar(2000) NOT NULL,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '1: waiting, 2: pending, 3:complete',
  `ctime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'create time',
  `ftime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Finish time',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

### Setup application
```
cd yourproject/
composer install
# make a symbol link.
ln -s /usr/local/php-5.4.44/bin/php /usr/local/bin/php
# create database config file.
echo '[common]
database_type = mysql
port          = 3306
charset       = utf8
[local : common]
server        = 127.0.0.1
username      = root
password      = <PASSWORD>
database_name = mail

[test : common]
[product : common]' > application/config/database.ini
# create mail config file.
echo '[common]
server.log_file        = /tmp/output.log
server.max_request     = 1000
server.worker_num      = 2
server.task_worker_num = 2
server.user            = nobody
server.group           = nobody

phpmail.charset    = "utf-8"
phpmail.host       = "smtp.gmail.com"
phpmail.smtpauth   = 1
phpmail.username   = {your_username}
phpmail.password   = {<PASSWORD>}
phpmail.smtpsecure = tls
phpmail.port       = 587
phpmail.from       = "<EMAIL>"
phpmail.fromname   = "<NAME>"
phpmail.wordwrap   = 100

[local : common]
[test : common]
[product : common]' > application/config/mail.ini
```

Usage
-----------
### start server.
```
./server.sh start
```

### telnet

协议采用空格或者制表符做分隔  
命令格式：CMD ARG1 ARG2 ARG3 ...  
返回格式：CODE RESPONSE  
注：若ARG/RESPONSE存在空格，需要通过 rawurlencode

```
[lancer@Lancer-MacBookPro ~]$ telnet 127.0.0.1 8001
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
send subject emailbody <EMAIL>;<EMAIL> <EMAIL>
0 129
```

上例发送主题：subject，主体内容：emailbody，发送邮件：user1;user2，抄送：user3  
CODE=0表示成功，129表示Email Id，可以通过 `status 129` 查询邮件是否异步发送完成  
CODE>0表示失败，message 通过协议方式追加状态码后。

### use php in client

```php
class Util_Mail {

    public static function send($subject, $body, $to, $cc='') {
        $client = new swoole_client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
        $client->set(['open_eof_check' => true, 'package_eof' => "\r\n"]);
        if ( ! $client->connect('127.0.0.1', 8001))
            return false;
{'fraction_non_alphanumeric': 0.14929474262685474, 'fraction_numerical': 0.03773584905660377, 'mean_word_length': 3.4975288303130148, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3241092', 'n_tokens_mistral': 2234, 'n_tokens_neox': 2144, 'n_words': 493}