php 邮件类,php邮件
php 邮件类,php邮件
编写一个用php socket 发送邮件的类,简单好用,当用到php程序发送邮件时,
而在163服务器中,可以在RCPT命令中还可以验证163邮箱是否存在,还有很多用处,
我现在暂时还没想到。
记录下,有需要的朋友可以参考下
class mail {
/**
* 这些即可发送email
*/
public $host;
public $port;
public $user;
public $password;
public $mail_form;
public $rcpt;
public $body;
/**
* 附属在 header 中信息
*/
public $to_name;
public $from_name;
public $subject;
public $html;
public $connection;
public $msg = array();
// 进行参数初始化
public function __construct($conf, $rcpt, $header, $body, $html = true) {
try {
$this->host = $conf['host'];
$this->port = $conf['port'];
$this->user = $conf['user'];
$this->password = $conf['password'];
$this->rcpt = $rcpt;
$this->to_name = $header['to_name'];
$this->from_name = $header['from_name'];
$this->subject = $header['subject'];
$this->html = $html;
$this->body = base64_encode($body);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function connect() {
$this->connection = @fsockopen($this->host, '25', $errno, $errstr, 10);
if (!$this->connection) {
throw new Exception('connect failed!');
}
$greet = $this->callback_code();
}
public function helo() {
if($this->is_connect() &&
$this->send_data('HELO ' . $this->host) &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function send_data($cmd) {
if (is_resource($this->connection)) {
return @fwrite($this->connection, $cmd."\r\n", strlen($cmd) + 2);
}
}
public function auth(){
if ($this->is_connect() &&
$this->send_data('AUTH LOGIN') && $this->callback_code() == 334 &&
$this->send_data(base64_encode($this->user)) && $this->callback_code() == 334 &&
$this->send_data(base64_encode($this->password)) && $this->callback_code() == 235 ) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function Mail() {
if ($this->is_connect() &&
$this->send_data("MAIL FROM:user>") &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function is_connect() {
return is_resource($this->connection);
}
public function callback_code() {
if ($this->is_connect()) {
$msg = fgets($this->connection);
if (!empty($msg)) {
$code = substr($msg, 0, strpos($msg, ' '));
} else {
return '';
}
$this->msg[] = $msg;
return $code;
}
}
public function get_callback_msg_lastest() {
return end($this->msg);
}
public function rcpt($rcpt) {
if ($this->is_connect() &&
$this->send_data("RCPT TO:") &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function data() {
if ($this->is_connect() &&
$this->send_data('DATA') &&
$this->callback_code() == 354) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function send_mail() {
try {
$this->connect();
$this->helo();
$this->auth();
$this->Mail();
if (is_array($this->rcpt)) {
foreach ($this->rcpt as $rcpt) {
$this->rcpt($rcpt);
}
} else {
$this->rcpt($this->rcpt);
}
$this->data();
$header = str_replace("\r\n" . '.', "\r\n" . '..', trim(implode("\r\n", $this->get_header())));
$body = str_replace("\r\n" . '.', "\r\n" . '..', $this->body);
$body = substr($body, 0, 1) == '.' ? '.' . $body : $body;
$this->send_data($header);
$this->send_data('');
$this->send_data($body);
$this->send_data('.');
if ($this->callback_code() != 250) {
throw new Exception('send mail falied!');
}
return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
//只能检测同一邮件服务器上email address
public function is_email_exist() {
}
public function get_header() {
$header = array();
$content_type = $this->html ? 'text/html;' : 'text/plain;' ;
$headers [] = 'Date: ' . gmdate('D, j M Y H:i:s') . ' +0000';
$to_mail = is_array($this->rcpt) ? implode(',', $this->rcpt) : $this->rcpt;
$headers [] = 'To: "' . '=? utf8?B?' . base64_encode($this->to_name) . '?=' . '" ';
$headers [] = 'From: "' . '=?utf8?B?' . base64_encode($this->from_name) . '?=' . '" user . '>';
$headers [] = 'Subject: ' . '=?utf8?B?' . base64_encode($this->subject) . '?=';
$headers [] = 'Content-Type:'.$content_type . ' charset=utf8; format=flowed';
$headers [] = 'Content-Transfer-Encoding: base64';
$headers [] = 'Content-Disposition: inline';
return $headers;
}
}
/**
* 用函数封装类
*/
function send_mail($conf, $rcpt, $header, $body) {
$mail = new mail($conf, $rcpt, $header, $body);
if ($mail->send_mail()) {
echo 'send email successfully!';
} else {
echo 'falied!';
}
}
$conf = array(
'host' => 'smtp.163.com',
'port' => '25',
'user' => 'z1298703836@163.com',
'password' => 'zeiwei123',
);
$rcpt = array(
'z1298703836@sina.com',
'wei.zen@baisonmail.com'
);
$header = array(
'to_name' => 'willstang',
'from_name' => 'sinawill',
'subject' => 'tst of reo ',
);
$body = '
sfsfsd
';$mail = new mail($conf, $rcpt, $header, $body);
$mail->send_mail();
echo '
';<br>print_r($mail->msg); <p> </p> <p>熟悉邮件发送的过程,它的stmp协议,</p> <p>还有一些发送的细节,如:发送的内容分为两部分, 一部分在头部,另一部分是email的正文,</p> <p> </p>

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Dans ce chapitre, nous comprendrons les variables d'environnement, la configuration générale, la configuration de la base de données et la configuration de la messagerie dans CakePHP.

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Pour travailler avec la date et l'heure dans cakephp4, nous allons utiliser la classe FrozenTime disponible.

Travailler avec la base de données dans CakePHP est très simple. Nous comprendrons les opérations CRUD (Créer, Lire, Mettre à jour, Supprimer) dans ce chapitre.

Pour travailler sur le téléchargement de fichiers, nous allons utiliser l'assistant de formulaire. Voici un exemple de téléchargement de fichiers.

CakePHP est un framework open source pour PHP. Il vise à faciliter grandement le développement, le déploiement et la maintenance d'applications. CakePHP est basé sur une architecture de type MVC à la fois puissante et facile à appréhender. Modèles, vues et contrôleurs gu

Dans ce chapitre, nous allons apprendre les sujets suivants liés au routage ?

Le validateur peut être créé en ajoutant les deux lignes suivantes dans le contrôleur.
