@@ -18,21 +18,43 @@ class MailSendJob extends BaseJob
1818 public $ emailUserName = null ;
1919 public $ option = [];
2020 public $ module ;
21+ public $ html ;
22+ public $ type ;
2123
22- public static function create ( $ email , $ subject , $ template , $ templateData = [], $ emailUserName = null , $ option = [], $ delay = 0 , $ module = null )
24+ private static function checkConfig ( )
2325 {
2426 $ provider = app ()->config ->get ('EmailSenderProvider ' );
2527 BizException::throwsIfEmpty ('邮箱发送未配置 ' , $ provider );
28+ }
29+
30+ public static function createHtml ($ email , $ subject , $ html , $ emailUserName = null , $ option = [], $ delay = 0 )
31+ {
32+ self ::checkConfig ();
33+ $ job = new static ();
34+ $ job ->type = 'html ' ;
35+ $ job ->email = $ email ;
36+ $ job ->subject = $ subject ;
37+ $ job ->html = $ html ;
38+ $ job ->emailUserName = $ emailUserName ;
39+ $ job ->option = $ option ;
40+ if ($ delay > 0 ) {
41+ $ job ->delay ($ delay );
42+ }
43+ app ('Illuminate\Contracts\Bus\Dispatcher ' )->dispatch ($ job );
44+ }
2645
27- $ job = new MailSendJob ();
46+ public static function create ($ email , $ subject , $ template , $ templateData = [], $ emailUserName = null , $ option = [], $ delay = 0 , $ module = null )
47+ {
48+ self ::checkConfig ();
49+ $ job = new static ();
2850 $ job ->email = $ email ;
2951 $ job ->subject = $ subject ;
3052 $ job ->template = $ template ;
3153 $ job ->templateData = $ templateData ;
3254 $ job ->emailUserName = $ emailUserName ;
3355 $ job ->option = $ option ;
3456 $ job ->module = $ module ;
35- if ($ delay > 0 ) {
57+ if ($ delay > 0 ) {
3658 $ job ->delay ($ delay );
3759 }
3860 app ('Illuminate\Contracts\Bus\Dispatcher ' )->dispatch ($ job );
@@ -45,29 +67,37 @@ public function handle()
4567 $ instance = MailSenderProvider::get ($ provider );
4668 Logger::info ('Email ' , 'Start ' , $ this ->email . ' -> ' . $ this ->subject . ' -> ' . $ this ->template );
4769
48- $ view = $ this ->template ;
49- if (!view ()->exists ($ view )) {
50- $ view = 'theme. ' . modstart_config ()->getWithEnv ('siteTemplate ' , 'default ' ) . '.mail. ' . $ this ->template ;
51- if (!view ()->exists ($ view )) {
52- $ view = 'theme.default.mail. ' . $ this ->template ;
70+ switch ($ this ->type ) {
71+ case 'html ' :
72+ $ html = $ this ->html ;
73+ break ;
74+ default :
75+ $ view = $ this ->template ;
5376 if (!view ()->exists ($ view )) {
54- if ($ this ->module ) {
55- $ view = 'module:: ' . $ this ->module . '.View.mail. ' . $ this ->template ;
56- }
77+ $ view = 'theme. ' . modstart_config ()->getWithEnv ('siteTemplate ' , 'default ' ) . '.mail. ' . $ this ->template ;
5778 if (!view ()->exists ($ view )) {
58- $ view = 'module::Vendor.View.mail. ' . $ this ->template ;
79+ $ view = 'theme.default.mail. ' . $ this ->template ;
80+ if (!view ()->exists ($ view )) {
81+ if ($ this ->module ) {
82+ $ view = 'module:: ' . $ this ->module . '.View.mail. ' . $ this ->template ;
83+ }
84+ if (!view ()->exists ($ view )) {
85+ $ view = 'module::Vendor.View.mail. ' . $ this ->template ;
86+ }
87+ }
5988 }
6089 }
61- }
62- }
63- if (!view ()->exists ($ view )) {
64- throw new \Exception ('mail view not found : ' . $ view );
65- }
66- if (null === $ this ->emailUserName ) {
67- $ this ->emailUserName = $ this ->email ;
90+ if (!view ()->exists ($ view )) {
91+ throw new \Exception ('mail view not found : ' . $ view );
92+ }
93+ if (null === $ this ->emailUserName ) {
94+ $ this ->emailUserName = $ this ->email ;
95+ }
96+ $ html = View::make ($ view , $ this ->templateData )->render ();
97+ break ;
6898 }
69- $ content = View:: make ( $ view , $ this -> templateData )-> render ( );
70- $ ret = $ instance ->send ($ this ->email , $ this ->emailUserName , $ this ->subject , $ content );
99+ BizException:: throwsIfEmpty ( ' MailSendJob.HtmlEmpty ' , $ html );
100+ $ ret = $ instance ->send ($ this ->email , $ this ->emailUserName , $ this ->subject , $ html );
71101 BizException::throwsIfResponseError ($ ret );
72102 Logger::info ('Email ' , 'End ' , $ this ->email . ' -> ' . $ this ->subject );
73103 }
0 commit comments