-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
119 lines (99 loc) · 2.47 KB
/
index.php
File metadata and controls
119 lines (99 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
require_once "Mail.php";
include('mime.php');
/*
* Send email from Gmail account with use Abstract Factory
* Author: Michael Makarenkov
*/
interface EmailFactory {
public function create();
}
class GmailFactory implements EmailFactory
{
public function create()
{
return new Gmail();
}
}
abstract class Email {
protected $obj;
protected $from;
protected $subj;
protected $to;
abstract public function setSubject($subject);
abstract public function setBody($body);
abstract public function setFrom($from);
abstract public function setTo($to);
abstract public function send();
public function setObj($obj)
{
$this->obj = $obj;
}
public function getObj()
{
return $this->obj;
}
}
class Gmail extends Email
{
public function __construct() {
$this->setObj(new Mail_mime("rn"));
}
public function setBody($body)
{
$this->body = $body;
}
public function setFrom($from)
{
$this->from = $from;
}
public function setTo($to)
{
$this->to = $to;
}
public function setSubject($subj)
{
$this->subj = $subj;
}
public function send()
{
$headers = array(
'From' => $this->from,
'To' => $this->to,
'Subject' => $this->subj
);
$headers = $this->getObj()->headers($headers);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'username',
'password' => 'password'
));
$body = $this->getObj()->get();
$mail = $smtp->send($this->to, $headers, $this->body);
if(PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
}
}
class Application {
public function __construct(EmailFactory $ef) {
$emailObj = $ef->create();
$emailObj->setSubject("this is subject");
$emailObj->setBody("this is body");
$emailObj->setFrom("from@test.com");
$emailObj->setTo("to@test.com");
$emailObj->send();
}
}
class ApplicationRunner {
public static function run()
{
$gmail = new GmailFactory();
new Application($gmail);
}
}
ApplicationRunner::run();