Skip to content

Commit

Permalink
Refactor some code into overridable methods.
Browse files Browse the repository at this point in the history
This should make the transport a little more extensibility friendly.
  • Loading branch information
ndm2 committed Mar 13, 2014
1 parent 0d4a6e3 commit 5326073
Showing 1 changed file with 74 additions and 17 deletions.
91 changes: 74 additions & 17 deletions lib/Cake/Network/Email/SmtpTransport.php
Expand Up @@ -153,38 +153,65 @@ protected function _auth() {
}

/**
* Send emails
* Prepares the `MAIL FROM` SMTP command.
*
* @return void
* @throws SocketException
* @param string $email The email address to send with the command.
* @return string
*/
protected function _sendRcpt() {
protected function _prepareFromCmd($email) {
return 'MAIL FROM:<' . $email . '>';
}

/**
* Prepares the `RCPT TO` SMTP command.
*
* @param string $email The email address to send with the command.
* @return string
*/
protected function _prepareRcptCmd($email) {
return 'RCPT TO:<' . $email . '>';
}

/**
* Prepares the `from` email address.
*
* @return array
*/
protected function _prepareFromAddress() {
$from = $this->_cakeEmail->returnPath();
if (empty($from)) {
$from = $this->_cakeEmail->from();
}
$this->_smtpSend('MAIL FROM:<' . key($from) . '>');
return $from;
}

/**
* Prepares the recipient email addresses.
*
* @return array
*/
protected function _prepareRecipientAddresses() {
$to = $this->_cakeEmail->to();
$cc = $this->_cakeEmail->cc();
$bcc = $this->_cakeEmail->bcc();
$emails = array_merge(array_keys($to), array_keys($cc), array_keys($bcc));
foreach ($emails as $email) {
$this->_smtpSend('RCPT TO:<' . $email . '>');
}
return array_merge(array_keys($to), array_keys($cc), array_keys($bcc));
}

/**
* Send Data
* Prepares the message headers.
*
* @return void
* @throws SocketException
* @return array
*/
protected function _sendData() {
$this->_smtpSend('DATA', '354');
protected function _prepareMessageHeaders() {
return $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'to', 'cc', 'subject'));
}

$headers = $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'to', 'cc', 'subject'));
$headers = $this->_headersToString($headers);
/**
* Prepares the message body.
*
* @return string
*/
protected function _prepareMessage() {
$lines = $this->_cakeEmail->message();
$messages = array();
foreach ($lines as $line) {
Expand All @@ -194,7 +221,37 @@ protected function _sendData() {
$messages[] = $line;
}
}
$message = implode("\r\n", $messages);
return implode("\r\n", $messages);
}

/**
* Send emails
*
* @return void
* @throws SocketException
*/
protected function _sendRcpt() {
$from = $this->_prepareFromAddress();
$this->_smtpSend($this->_prepareFromCmd(key($from)));

$emails = $this->_prepareRecipientAddresses();
foreach ($emails as $email) {
$this->_smtpSend($this->_prepareRcptCmd($email));
}
}

/**
* Send Data
*
* @return void
* @throws SocketException
*/
protected function _sendData() {
$this->_smtpSend('DATA', '354');

$headers = $this->_headersToString($this->_prepareMessageHeaders());
$message = $this->_prepareMessage();

$this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
$this->_content = array('headers' => $headers, 'message' => $message);
}
Expand Down

0 comments on commit 5326073

Please sign in to comment.