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
|
|
|
use SilverStripe\Dev\BuildTask; |
9
|
|
|
|
10
|
|
|
class SendMailTest extends BuildTask |
11
|
|
|
{ |
12
|
|
|
protected $title = 'Test if emails are working'; |
13
|
|
|
|
14
|
|
|
private static $segment = 'testemail'; |
15
|
|
|
|
16
|
|
|
public function run($request) |
17
|
|
|
{ |
18
|
|
|
$from = $request->getVar('from') ?: 'webmaster@' . Director::host(); |
19
|
|
|
$to = $request->getVar('to') ?: 'support@' . Director::host(); |
20
|
|
|
$subject = $request->getVar('subject') ?: 'testing email'; |
21
|
|
|
$message = $request->getVar('message') ?: 'Message goes here'; |
22
|
|
|
|
23
|
|
|
echo ' |
24
|
|
|
<style> |
25
|
|
|
input {width: 80vw; max-width: 500px; padding: 5px;} |
26
|
|
|
</style> |
27
|
|
|
<form action="" method="get"> |
28
|
|
|
from: <br/><input name="from" value="' . Convert::raw2att($from) . '" /><br/><br/> |
|
|
|
|
29
|
|
|
to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/> |
|
|
|
|
30
|
|
|
subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/> |
|
|
|
|
31
|
|
|
message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/> |
|
|
|
|
32
|
|
|
<input type="submit" /> |
33
|
|
|
</form> |
34
|
|
|
'; |
35
|
|
|
if(isset($request->getVar('from'))) { |
36
|
|
|
echo '<h1>Outcome</h1>'; |
37
|
|
|
$outcome = mail($to, $subject, $message); |
38
|
|
|
echo 'PHP mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine(); |
39
|
|
|
$email = new Email($from, $to, $subject, $message); |
40
|
|
|
$outcome = $email->sendPlain(); |
41
|
|
|
echo 'Silverstripe e-mail sent: ' . ($outcome ? 'YES' : 'NO') . $this->newLine(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function newLine() |
46
|
|
|
{ |
47
|
|
|
if (Director::is_cli()) { |
48
|
|
|
return ' |
49
|
|
|
|
50
|
|
|
'; |
51
|
|
|
} |
52
|
|
|
return '<br /><br />'; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|