137 lines
4.2 KiB
PHP
137 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace app\api\job;
|
|
|
|
use Exception;
|
|
use think\Db;
|
|
use think\Log;
|
|
use think\queue\Job;
|
|
|
|
class domail {
|
|
|
|
public function fire(Job $job, $data) {
|
|
// $this->send($data);
|
|
// $job->delete();
|
|
|
|
// 发送邮件
|
|
try{
|
|
$this->send($data);
|
|
$job->delete();
|
|
// $isJobDone = $this->doTask($data);
|
|
}catch(Exception $e){
|
|
$this->addErrMsg($data);
|
|
$job->delete();
|
|
}
|
|
|
|
// 删除
|
|
// $job->delete();
|
|
//入库
|
|
// $this->insertData($data,$isJobDone);
|
|
|
|
}
|
|
|
|
|
|
// /**
|
|
// * 发送邮件
|
|
// * @param $data
|
|
// * @return array
|
|
// */
|
|
// public function doTask($data){
|
|
// if(isset($data['attachment_url']) && !empty($data['attachment_url']) ){
|
|
// $res = sendEmail($data['email'],$data['title'],$data['from_name'],$data['content'],$data['sendEmail'],$data['sendPassword'],$data['attachment_url']);
|
|
// }else{
|
|
// $res = sendEmail($data['email'],$data['title'],$data['from_name'],$data['content'],$data['sendEmail'],$data['sendPassword']);
|
|
// }
|
|
// return $res;
|
|
// }
|
|
|
|
|
|
// /**
|
|
// * email记录入库
|
|
// * @param $data
|
|
// * @param $isJobDone
|
|
// */
|
|
// public function insertData($data,$isJobDone)
|
|
// {
|
|
// $insert = [
|
|
// 'title' => $data['title'],
|
|
// 'content' => $data['content'],
|
|
// 'recive_id' => $data['user_id'],
|
|
// 'recive_email' => $data['email'],
|
|
// 'journal_id' => $data['journal_id'],
|
|
// 'journal_email' => $data['sendEmail'],
|
|
// 'journal_password' => $data['sendPassword'],
|
|
// 'create_time' => time()
|
|
// ];
|
|
// if (isset($data['attachment_url']) && !empty($data['attachment_url'])) {
|
|
// $insert['is_attachment'] = 1;
|
|
// $insert['attachment_url'] = $data['attachment_url'];
|
|
// }
|
|
// if($isJobDone['status']==0){
|
|
// $insert['is_success'] = 0;
|
|
// $insert['fail_reason'] = $isJobDone['data'];
|
|
// }
|
|
// Db::name('email')->insert($insert);
|
|
// }
|
|
|
|
|
|
/**
|
|
* 添加邮件错误记录
|
|
*/
|
|
private function addErrMsg($data){
|
|
$insert = [
|
|
'title' => $data['title'],
|
|
'content' => $data['content'],
|
|
'recive_id' => $data['user_id'],
|
|
'recive_email' => $data['email'],
|
|
'journal_id' => $data['journal_id'],
|
|
'journal_email' => $data['sendEmail'],
|
|
'journal_password' => $data['sendPassword'],
|
|
'create_time' => time()
|
|
];
|
|
if (isset($data['attachment_url']) && !empty($data['attachment_url'])) {
|
|
$insert['is_attachment'] = 1;
|
|
$insert['attachment_url'] = $data['attachment_url'];
|
|
}
|
|
$insert['is_success'] = 0;
|
|
$insert['fail_reason'] = "system error at push email queue!";
|
|
Db::name('email')->insert($insert);
|
|
}
|
|
|
|
/**
|
|
* 发送邮件的逻辑
|
|
* @param type $data
|
|
*/
|
|
public function send($data){
|
|
$insert = [
|
|
'title'=>$data['title'],
|
|
'content'=>$data['content'],
|
|
'recive_id'=>$data['user_id'],
|
|
'recive_email'=>$data['email'],
|
|
'journal_id'=>$data['journal_id'],
|
|
'journal_email'=>$data['sendEmail'],
|
|
'journal_password'=>$data['sendPassword'],
|
|
'create_time'=>time()
|
|
];
|
|
if(isset($data['attachment_url']) && !empty($data['attachment_url']) ){
|
|
$res = sendEmail($data['email'],$data['title'],$data['from_name'],$data['content'],$data['sendEmail'],$data['sendPassword'],$data['attachment_url']);
|
|
$insert['is_attachment'] = 1;
|
|
$insert['attachment_url'] = $data['attachment_url'];
|
|
}else{
|
|
$res = sendEmail($data['email'],$data['title'],$data['from_name'],$data['content'],$data['sendEmail'],$data['sendPassword']);
|
|
}
|
|
|
|
if($res['status']==1){
|
|
Db::name('email')->insert($insert);
|
|
return true;
|
|
}else{
|
|
$insert['is_success'] = 0;
|
|
$insert['fail_reason'] = $res['data'];
|
|
Db::name('email')->insert($insert);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
}
|