Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor some code into overridable methods.

This should make the transport a little more extensibility friendly.
  • Loading branch information...
commit 5326073c80367c243e523befc39271a9e03172cb 1 parent 0d4a6e3
ndm2 ndm2 authored
Showing with 74 additions and 17 deletions.
  1. +74 −17 lib/Cake/Network/Email/SmtpTransport.php
91 lib/Cake/Network/Email/SmtpTransport.php
View
@@ -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) {
@@ -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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.