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