前言
招商银行的收款通知可以添加邮件推送,因此利用php-imap收取邮件,可达到自动化确款的目的
目标
前置条件
composer require php-imap/php-imap
正式内容
php-imap 的使用
$mailbox = new Mailbox(
'{SMTP服务器:SMTP端口号/imap}INBOX', // IMAP server and mailbox folder
'邮箱账号', // Username for the before configured mailbox
'邮箱密码', // Password for the before configured username
null, // Directory, where attachments will be saved (optional)
'UTF-8' // Server encoding (optional)
);
try {
$mailsIds = $mailbox->searchMailbox('ALL');
} catch (PhpImap\Exceptions\ConnectionException $ex) {
echo "IMAP connection failed: " . $ex;
die();
}
rsort($mailsIds);
foreach ($mailsIds as $key => $item) {
//循环遍历
$mail = $mailbox->getMail($item);
$fromAddress = $mail->fromAddress;
$content = $mail->textHtml;
if(!$content){
$content = $mail->textPlain;
}
// $mail->subject;标题
// $mail->date;发件日期
}
对招商银行的邮件进行内容提取和匹配
private function CmbchinaFormat($content){
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日)他行实时转入人民币([\d\.]+),付方(\S*)/",$content,$matches);
if(!empty($matches)){
return ['price'=>$matches[3],'card'=>$matches[1],'payed_name'=>$matches[4],];
}
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日\d{2}:\d{2})二维码收款\((\S*)\),人民币([\d\.]+)/",$content,$matches);
if(!empty($matches)){
return ['price'=>$matches[4],'card'=>$matches[1],'payed_name'=>$matches[3],];
}
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日\d{2}:\d{2})收款人民币([\d\.]*),备注:(\S*)/", $content, $matches);
if(!empty($matches)){
return ['price'=>$matches[3],'card'=>$matches[1],'payed_name'=>str_replace(",更多详情请查看招商银行APP动账通知。", "", $matches[4]),];
}
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日\d{2}:\d{2})银联入账人民币([\d\.]*)元\((\S*)\)/", $content, $matches);
if(!empty($matches)){
return ['price'=>$matches[3],'card'=>$matches[1],'payed_name'=>$matches[4],];
}
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日\d{2}:\d{2})转账汇款人民币([\d\.]*),手续费人民币([\d\.]*),收款人:马颂新,请以收款人实际入账为准/", $content, $matches);
if(!empty($matches)){
return ['price'=>$matches[3],'card'=>$matches[1],'payed_name'=>'',];
}
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日)收到汇款人民币([\d\.]*),付方(\S*),账号尾号(\d{4}),备注:(\S*)/", $content, $matches);
if(!empty($matches)){
return ['price'=>$matches[3],'card'=>$matches[1],'payed_name'=>$matches[4],'remark'=>$matches[6],'payed_card'=>$matches[5]];
}
preg_match("/您账户(\d{4})于(\d{2}月\d{2}日\d{2}:\d{2})银联转入人民币([\d\.]*)/", $content, $matches);
if(!empty($matches)){
return ['price'=>$matches[3],'card'=>$matches[1],'payed_name'=>'','remark'=>'','payed_card'=>''];
}
return false;
}