mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Added code to allow Email compoenent to send email via SMTP, along with test for component
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5351 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
3f25e0c44d
commit
15da6d1255
2 changed files with 85 additions and 17 deletions
|
@ -159,6 +159,13 @@ class EmailComponent extends Object{
|
|||
'host' => 'localhost',
|
||||
'timeout' => 30);
|
||||
|
||||
/**
|
||||
* SMTP errors variable
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $smtpError = null;
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
|
@ -500,19 +507,71 @@ class EmailComponent extends Object{
|
|||
* @access private
|
||||
*/
|
||||
function __smtp() {
|
||||
$smtpConnect = fsockopen($this->smtpOptions['host'], $this->smtpOptions['port'], $errno, $errstr, $this->smtpOptions['timeout']);
|
||||
$this-> smtpConnection = @fsockopen($this->smtpOptions['host'], $this->smtpOptions['port'], $errno, $errstr, $this->smtpOptions['timeout']);
|
||||
$this->__getSmtpResponse();
|
||||
|
||||
if (!$smtpConnect) {
|
||||
if (!$this->smtpConnection) {
|
||||
return false;
|
||||
}
|
||||
|
||||
fputs($smtpConnect, "HELO {$this->smtpOptions['host']}" . $this->_newLine);
|
||||
fputs($smtpConnect, "MAIL FROM: {$this->smtpOptions['from']}" . $this->_newLine);
|
||||
fputs($smtpConnect, "RCPT TO: {$this->to}" . $this->_newLine);
|
||||
fputs($smtpConnect, "DATE" . $this->_newLine);
|
||||
fputs($smtpConnect, "To: {$this->to}\r\nFrom: {$this->smtpOptions['from']}\r\n{$this->subject}\r\n{$this->__header}\r\n\r\n{$this->__message}\r\n");
|
||||
fputs($smtpConnect, "QUIT" . $this->_newLine);
|
||||
fclose($smtpConnect);
|
||||
if (!$this->__sendData("HELO cake")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->__sendData("MAIL FROM: {$this->from}\r\n")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->__sendData("RCPT TO: {$this->to}\r\n")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->__sendData("DATA\r\n");
|
||||
|
||||
if (!$this->__sendData("To: {$this->to}\r\n")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->__sendData("{$this->__header}\r\n")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->__sendData("{$this->__message}\r\n")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->__sendData("QUIT\r\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private method for getting SMTP response
|
||||
*/
|
||||
function __getSmtpResponse() {
|
||||
$response = @fgets($this->smtpConnection, 512);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private method for sending data to SMTP connection
|
||||
*
|
||||
* @param string data
|
||||
*/
|
||||
function __sendData($data) {
|
||||
@fputs($this->smtpConnection, $data);
|
||||
$response = $this->__getSmtpResponse();
|
||||
|
||||
/**
|
||||
* If there is a 250 in the response code, that means
|
||||
* everything went ok
|
||||
*/
|
||||
if (stristr($response, '250') !== false) {
|
||||
echo "Failed while sending:<pre>{$data}</pre>";
|
||||
$this->smtpError = $response;
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Enter description here...
|
||||
|
@ -522,7 +581,15 @@ class EmailComponent extends Object{
|
|||
*/
|
||||
function __debug() {
|
||||
$fm = '<pre>';
|
||||
|
||||
if ($this->delivery == 'smtp') {
|
||||
$fm .= sprintf('%s %s', 'Host:', $this->smtpOptions['host']);
|
||||
$fm .= sprintf('%s %s', 'Port:', $this->smtpOptions['port']);
|
||||
$fm .= sprintf('%s %s', 'Timeout:', $this->smtpOptions['timeout']);
|
||||
}
|
||||
|
||||
$fm .= sprintf('%s %s', 'To:', $this->to);
|
||||
$fm .= sprintf('%s %s', 'From:', $this->from);
|
||||
$fm .= sprintf('%s %s', 'Subject:', $this->subject);
|
||||
$fm .= sprintf('%s\n\n%s', 'Header:', $this->__header);
|
||||
$fm .= sprintf('%s\n\n%s', 'Parameters:', $this->additionalParams);
|
||||
|
|
|
@ -48,14 +48,15 @@ class EmailTest extends CakeTestCase {
|
|||
ClassRegistry::addObject('view', new View($this->Controller));
|
||||
}
|
||||
|
||||
function testConstruction() {
|
||||
$this->assertTrue(is_object($this->Controller->Email));
|
||||
function testSmtpSend() {
|
||||
$this->Controller->Email->to = 'chartjes@localhost';
|
||||
$this->Controller->Email->subject = 'Cake SMTP test';
|
||||
$this->Controller->Email->replyTo = 'noreply@example.com';
|
||||
$this->Controller->Email->from = 'noreply@example.com';
|
||||
$this->Controller->Email->delivery = 'smtp';
|
||||
$this->Controller->Email->template = null;
|
||||
$this->assertTrue($this->Controller->Email->send("This is the body of the message"));
|
||||
}
|
||||
|
||||
function testBadSmtpSent() {
|
||||
$this->Controller->Email->smtpOptions['host'] = 'caketest.com';
|
||||
$this->Controller->Email->delivery = 'smtp';
|
||||
$this->assertFalse($this->Controller->Email->send('This should not work'));
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue