爱吃醋的饺子's BLOG

浏览数(8669)

【PHPMail极简版】

PHPMailer也太臃肿了(我极度讨厌复杂化),Php发送邮件本来的样子。

不明白的是为何你情愿让风尘刻画你的样子?不明白的是为何人世间总不能溶解你的样子?

简单的世界才是世界本来的样子

https://github.com/PHPMailer/PHPMailer/

/**

* 极简邮件发送,也许发送一下文字就够了

* @Author Dumplings

* @DateTime 2021-08-18T22:02:29+0800

* @Email [i@xiv.cm]

* @Version [1.0]

* @return [type]

*/

class Email

{

private $socket;

public function __construct($config=[]){

$this->conf=$config;

//邮件地址

$this->conf['user'] = base64_encode($this->conf['user']);

//邮件密码

$this->conf['pwd'] = base64_encode($this->conf['pwd']);

//连接服务器

$this->socket = stream_socket_client("tcp://{$this->conf['host']}:{$this->conf['port']}",$errno,$errstr,30);

if($this->conf['port']==456){// 将通讯管道设置为加密模式 SSL加密方式 某家服务器一直无法用加密

//stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

stream_socket_enable_crypto($this->socket, true);

}

// 获取服务器的状态码

$response = fgets($this->socket);

// 服务器返回状态码不是220 则连接失败

if(strstr($response,'220') === false){

throw new Exception("stream_socket_client fail errno={$errno} errstr={$errstr}");

}

}

//执行服务器操作

public function push($cmd,$return_code){

$result = fwrite($this->socket,$cmd);

if(!$result){

return false;

}

// 获取服务器的状态码

$response = fgets($this->socket);

if(strstr($response,"{$return_code}") === false){

return $response;

}

}

// 组合命令发送邮件

public function send_mail($from,$to,$subject,$body){

$info = "From:{$from}\r\n";

$info .= "To:{$to}\r\n";

$info .= "Subject:$subject\r\n";

$info .= "Content-Type:text/html;\r\n";

$info .= "charset=gb2312\r\n\r\n";

$info .= $body;

$this->push("HELO {$this->conf['host']}\r\n",250);

$this->push("AUTH LOGIN\r\n",334);

$this->push("{$this->conf['user']}\r\n",334);

$this->push("{$this->conf['pwd']}\r\n",235);

$this->push("MAIL FROM: <{$from}>\r\n",250);

$this->push("RCPT TO: <{$to}>\r\n",250);

$this->push("DATA\r\n",354);

$this->push($info."\r\n.\r\n",250);

$this->push("QUIT\r\n",221);

return true;

}

//关闭通讯管道设置为加密模式,套接子

public function close(){

stream_socket_enable_crypto($this->socket, false);

fclose($this->socket);

return true;

}

}

$email = new Email(['host'=>'smtp.qq.com','port'=>25,'user'=>'send@xiv.cm','pwd'=>'******']);

$email->send_mail('send@xiv.cm', 'receive@xiv.cm','我是邮件标题', '我是邮件内容');

?>

✎﹏₯㎕﹍﹍·狗粮

微信打赏

菜单