| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\EmailTest\Tasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Control\Director; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\Email\Email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Dev\BuildTask; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @coversNothing | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class SendMailTest extends BuildTask | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     protected $title = 'Test if emails are working'; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     private static $segment = 'testemail'; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function run($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         // <<<<<<< HEAD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | //         $from = $_GET['from'] ?? 'webmaster@' . Director::host(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | //         $to = $_GET['to'] ?? 'support@' . Director::host(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | //         $subject = $_GET['subject'] ?? 'testing email'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | //         $message = $_GET['message'] ?? 'Message goes here'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | //         echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | //             <style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | //                 input {width: 80vw; max-width: 500px; padding: 5px;} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | //             </style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | //             <form action="" method="get"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | //                 from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | //                 to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | //                 subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | //                 message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | //                 <input type="submit" /> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | //             </form> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | //             <h1>Outcome</h1> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | //         '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | //         $outcome = mail($to, $subject, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | //         echo 'PHP mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | //         $email = new Email($from, $to, $subject, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | //         $outcome = $email->sendPlain(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | //         echo 'Silverstripe e-mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         // ======= | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /** @var Kernel $kernel */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $kernel = Injector::inst()->get(Kernel::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $kernel->setEnvironment('dev'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $adminEmail = Config::inst()->get(Email::class, 'admin_email'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (is_array($adminEmail)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $keys = array_keys($adminEmail); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $adminEmail = array_pop($keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $from = $request->getVar('from') ?: $adminEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $to = $request->getVar('to') ?: $adminEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $subject = $request->getVar('subject') ?: 'testing email'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $message = $request->getVar('message') ?: 'Message goes here'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | from: ' . (string) Convert::raw2att($from) . ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | to: ' . (string) Convert::raw2att($to) . ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | subject:' . (string) Convert::raw2att($subject) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | message: ' . (string) Convert::raw2att($message) . ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | Change values like this: sake dev/tasks/testemail [email protected] [email protected] subject=test message=hello | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 <style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     input {width: 80vw; max-width: 500px; padding: 5px;} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 </style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 <form action="" method="get"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     <input type="submit" /> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 </form> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if ($request->getVar('from')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | ========================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | Outcome | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | ========================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 echo '<h1>Outcome</h1>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $outcome = mail($to, $subject . ' raw mail', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             echo 'PHP mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $email = new Email($from, $to, $subject . ' silverstripe message', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $outcome = $email->sendPlain(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } catch(\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 die('<div>Mail send error: <span style="color:red">'.$e->getMessage().'</span></div>'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |             echo 'Silverstripe e-mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine(); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function newLine() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |         return '<br /><br />'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |