| 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 |  |  | use Symfony\Component\Mailer\MailerInterface; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @coversNothing | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | class SendMailTest extends BuildTask | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     protected $title = 'Test if emails are working'; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     private static $segment = 'testemail'; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function run($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         /** @var Kernel $kernel */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $kernel = Injector::inst()->get(Kernel::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $kernel->setEnvironment('dev'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $adminEmail = Config::inst()->get(Email::class, 'admin_email'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if (is_array($adminEmail)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $keys = array_keys($adminEmail); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $adminEmail = array_pop($keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $from = $request->requestVar('from') ?: $adminEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $to = $request->requestVar('to') ?: $adminEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $subject = $request->requestVar('subject') ?: 'testing email'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $message = $request->requestVar('message') ?: 'Message goes here'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $mailProvider = Injector::inst()->get(MailerInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | from: ' . Convert::raw2att($from) . ' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | to: ' . Convert::raw2att($to) . ' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | subject:' . Convert::raw2att($subject) . '" /><br/><br/> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | message: ' . Convert::raw2att($message) . ' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | Change values like this: sake dev/tasks/testemail [email protected] [email protected] subject=test message=hello | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 <style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     input {width: 80vw; max-width: 500px; padding: 5px;} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 </style> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 <form action="" method="'.$this->formMethod().'"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     <input type="submit" /> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 </form> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($request->requestVar('from')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 echo ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | ========================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | Outcome | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | ========================== | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 echo '<h1>Outcome</h1>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $outcome = mail($to, $subject . ' raw mail', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             echo 'PHP mail sent: ' . ($outcome ? 'NO' : 'CHECK EMAIL TO VERIFY') . $this->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $email = new Email($from, $to, $subject . ' silverstripe message', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $outcome = $email->sendPlain(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 die('<div>Mail send error: <span style="color:red">' . $e->getMessage() . '</span></div>'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             echo 'Silverstripe e-mail sent: ' . ($outcome ? 'NO' : 'CHECK EMAIL TO VERIFY') . $this->newLine(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |             echo 'Mail Service Provider: ' . get_class($mailProvider) . $this->newLine(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     protected function newLine(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         if (Director::is_cli()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             return ' | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             '; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         return '<br /><br />'; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected function formMethod(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return 'get'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  |         return 'post'; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths