Passed
Push — master ( 6c61ad...b39b69 )
by Nicolaas
06:50 queued 04:24
created

SendMailTest::run()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 5
b 0
f 0
nc 2
nop 1
dl 0
loc 26
rs 8.4444
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/>
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($from) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
                from: <br/><input name="from" value="' . /** @scrutinizer ignore-type */ Convert::raw2att($from) . '" /><br/><br/>
Loading history...
29
                to: <br/><input name="to" value="' . Convert::raw2att($to) . '" /><br/><br/>
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($to) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
                to: <br/><input name="to" value="' . /** @scrutinizer ignore-type */ Convert::raw2att($to) . '" /><br/><br/>
Loading history...
30
                subject: <br/><input name="subject" value="' . Convert::raw2att($subject) . '" /><br/><br/>
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($subject) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
                subject: <br/><input name="subject" value="' . /** @scrutinizer ignore-type */ Convert::raw2att($subject) . '" /><br/><br/>
Loading history...
31
                message: <br/><input name="message" value="' . Convert::raw2att($message) . '" /><br/><br/>
0 ignored issues
show
Bug introduced by
Are you sure SilverStripe\Core\Convert::raw2att($message) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
                message: <br/><input name="message" value="' . /** @scrutinizer ignore-type */ Convert::raw2att($message) . '" /><br/><br/>
Loading history...
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