Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/people/mail/PhabricatorPeopleMailEngine.php
12256 views
1
<?php
2
3
abstract class PhabricatorPeopleMailEngine
4
extends Phobject {
5
6
private $sender;
7
private $recipient;
8
private $recipientAddress;
9
private $activityLog;
10
11
final public function setSender(PhabricatorUser $sender) {
12
$this->sender = $sender;
13
return $this;
14
}
15
16
final public function getSender() {
17
if (!$this->sender) {
18
throw new PhutilInvalidStateException('setSender');
19
}
20
return $this->sender;
21
}
22
23
final public function setRecipient(PhabricatorUser $recipient) {
24
$this->recipient = $recipient;
25
return $this;
26
}
27
28
final public function getRecipient() {
29
if (!$this->recipient) {
30
throw new PhutilInvalidStateException('setRecipient');
31
}
32
return $this->recipient;
33
}
34
35
final public function setRecipientAddress(PhutilEmailAddress $address) {
36
$this->recipientAddress = $address;
37
return $this;
38
}
39
40
final public function getRecipientAddress() {
41
if (!$this->recipientAddress) {
42
throw new PhutilInvalidStateException('recipientAddress');
43
}
44
return $this->recipientAddress;
45
}
46
47
final public function hasRecipientAddress() {
48
return ($this->recipientAddress !== null);
49
}
50
51
final public function setActivityLog(PhabricatorUserLog $activity_log) {
52
$this->activityLog = $activity_log;
53
return $this;
54
}
55
56
final public function getActivityLog() {
57
return $this->activityLog;
58
}
59
60
final public function canSendMail() {
61
try {
62
$this->validateMail();
63
return true;
64
} catch (PhabricatorPeopleMailEngineException $ex) {
65
return false;
66
}
67
}
68
69
final public function sendMail() {
70
$this->validateMail();
71
$mail = $this->newMail();
72
73
if ($this->hasRecipientAddress()) {
74
$recipient_address = $this->getRecipientAddress();
75
$mail->addRawTos(array($recipient_address->getAddress()));
76
} else {
77
$recipient = $this->getRecipient();
78
$mail->addTos(array($recipient->getPHID()));
79
}
80
81
$activity_log = $this->getActivityLog();
82
if ($activity_log) {
83
$activity_log->save();
84
85
$body = array();
86
$body[] = rtrim($mail->getBody(), "\n");
87
$body[] = pht('Activity Log ID: #%d', $activity_log->getID());
88
$body = implode("\n\n", $body)."\n";
89
90
$mail->setBody($body);
91
}
92
93
$mail
94
->setForceDelivery(true)
95
->save();
96
97
return $mail;
98
}
99
100
abstract public function validateMail();
101
abstract protected function newMail();
102
103
final protected function throwValidationException($title, $body) {
104
throw new PhabricatorPeopleMailEngineException($title, $body);
105
}
106
107
final protected function newRemarkupText($text) {
108
$recipient = $this->getRecipient();
109
110
$engine = PhabricatorMarkupEngine::newMarkupEngine(array())
111
->setConfig('viewer', $recipient)
112
->setConfig('uri.base', PhabricatorEnv::getProductionURI('/'))
113
->setMode(PhutilRemarkupEngine::MODE_TEXT);
114
115
$rendered_text = $engine->markupText($text);
116
$rendered_text = rtrim($rendered_text, "\n");
117
118
return $rendered_text;
119
}
120
121
}
122
123