GithubHelp home page GithubHelp logo

weiyixi / phpmail-smtp-pop3-imap Goto Github PK

View Code? Open in Web Editor NEW
33.0 3.0 9.0 115 KB

邮件接收发送 PHP Mail receiving and sending (PHP POP3 IMAP SMTP)

PHP 100.00%
php pop3-client imap-client smtp-client smtp-mail imap-mailbox

phpmail-smtp-pop3-imap's Introduction

PhpMail-SMTP-POP3-IMAP 邮件接收发送 Mail receiving and sending

最近遇到一个工程需要自动发件(SMTP)收件(IMAP/POP3)功能,发现很难找到一个PHP的完整包来使用
所以自己综合phpMailer,PhpImap,csdn博客等各开源代码,做一个收发件都包括的工程,供新人使用。
有不足之处请指出

imap 收件示例

require_once __DIR__ . '/src/__autoload.php';
set_time_limit(0);
//IMAP收件
//argument is the directory into which attachments are to be saved:
$mailbox = new \src\PhpImap\Mailbox('{imap.qq.com:993/imap/ssl}INBOX', '[email protected]', 'XXXXXXX', __DIR__);
//读取所有邮件id到数组:
//Read all messaged into an array:
$mailsIds = $mailbox->searchMailbox('ALL');
if (!$mailsIds) {
    die('Mailbox is empty');
}
// Get the first message and save its attachment(s) to disk:
//获取一份邮件对象 打印出来
$mail = $mailbox->getMail($mailsIds[0]);
var_dump($mail);
die;

pop3 收件示例

require_once __DIR__ . '/src/__autoload.php';
set_time_limit(0);

$host = "tls://pop.126.com"; //‘tls://’为ssl协议加密,端口走加密端口
$user = "[email protected]"; //邮箱
$pass = "XXXXXX"; //密码
$rec = new \src\pop3\Pop3($host, 995, 3);
//打开连接
if (!$rec->open())
    die($rec->err_str);
echo "open ";
//登录
if (!$rec->login($user, $pass)) {
    var_dump($rec->err_str);
    die;
}
echo "login";

if (!$rec->stat())
    die($rec->err_str);
echo "You  have" . $rec->messages . "emails,total size:" . $rec->size . "<br>";
if ($rec->messages > 0) {
    //读取邮件列表
    if (!$rec->listmail())
        die($rec->err_str);
    echo "Your mail list:<br>";
    for ($i = 1; $i <= count($rec->mail_list); $i++) {
        echo "mailId:" . $rec->mail_list[$i]['num'] . "Size:" . $rec->mail_list[$i]['size'] . "<BR>";
    }
    //获取一个邮件
    //read One email
    $rec->getmail(1);
    echo "getHeader:<br>";
    for ($i = 0; $i < count($rec->head); $i++) {
        echo htmlspecialchars($rec->head[$i]) . "<br>\n";
    }

    echo "getContent:<BR>";
    for ($i = 0; $i < count($rec->body); $i++) {
        echo htmlspecialchars($rec->body[$i]) . "<br>\n";
    }
}
$rec->close();

die;

SMTP 发件

require_once __DIR__ . '/src/__autoload.php';
set_time_limit(0);

/**
 * @param $sendto_email  收件地址
 * @param $sendto_name   收件人
 * @param $subject       邮件标题
 * @param $body          邮件内容
 * @param $user_name     送件人
 * @param $post_array =[
 *          'server_address'=>'smtp.qq.com',    //smtp  服务器
 *          'port'=>465,                        //端口
 *          'mail_id'=>'[email protected]',          //账号
 *          'mail_pwd'=>'密码',                   //密码
 *          'mail_address'=>'发件人邮箱'
 * ]
 * @return bool
 * @throws \src\PHPMailer\Exception
 */
var_dump(smtp_mail('[email protected]', 'XXXX', 'hahah', 'aaaaaa', 'XXX', [
	'server_address' => 'smtp.126.com',    //smtp  服务器
	'port' => 465,                        //端口
	'mail_id' => '[email protected]',          //账号
	'mail_pwd' => 'XXXXX',                   //密码
	'mail_address' => '[email protected]'
]));
function smtp_mail($sendto_email, $sendto_name, $subject, $body, $user_name, $post_array,$is_html=false)
{
	$mail = new \src\PHPMailer\PHPMailer();
	$mail->SMTPDebug = 0;   // 开启Debug
	$mail->IsSMTP();                // 使用SMTP模式发送新建
	$mail->Host = $post_array['server_address']; // QQ企业邮箱SMTP服务器地址
	$mail->Port = $post_array['port'];  //邮件发送端口,一定是465
	$mail->SMTPAuth = true;         // 打开SMTP认证,本地搭建的也许不会需要这个参数
	$mail->SMTPSecure = "ssl";  // 打开SSL加密,这一句一定要
	$mail->Username = $post_array['mail_id'];   // SMTP用户名
	$mail->Password = $post_array['mail_pwd'];        // 为QQ邮箱SMTP的独立密码,即授权码
	$mail->From = $post_array['mail_address'];      // 发件人邮箱
	$mail->FromName = $user_name;// $post_array['mail_address'];  // 发件人

	$mail->CharSet = "UTF-8";            // 这里指定字符集!
	$mail->Encoding = "base64";
	if (!$mail->AddAddress($sendto_email, $sendto_name)) {
		return false;
	}  // 收件人邮箱和姓名
	$mail->addCC($post_array['mail_address']);//抄送
	$mail->IsHTML($is_html);  // send as HTML
	// 邮件主题
	$mail->Subject = $subject;

	$mail->Body = $body;      //isHTML是true 的时候  这里body有效
	$mail->AltBody = str_replace("<br/>", "\r\n", $body);  //isHTML是false 的时候  这里body有效
	if (!$mail->Send()) {
		return false;
	} else {
		return true;
	}
}

phpmail-smtp-pop3-imap's People

Contributors

weiyixi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

phpmail-smtp-pop3-imap's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.