jump to navigation

Sending email through SMTP February 11, 2006

Posted by rossoft in CakePHP.
trackback

In my webhost provider, the function mail() doesn’t work. I have to send email through a SMTP, but PHP doesn’t support this directly (only in Windows, and you have to touch the php.ini)

1. Copy the following code to /app/controllers/components/email.php

<?php

/*

* EmailComponent for sending email through a SMTP server

*

* @author      rossoft

* @version     0.1

* @license		MIT

*

*/
//EDIT THIS PARAMS
define('CONFIG_SMTP_HOST','localhost');
define('CONFIG_SMTP_USER','rossoft');
define('CONFIG_SMTP_PASS','password');
define('CONFIG_SMTP_EMAIL','rossoft@myhost.com');

define('PHPMAILER_SUBDIR','phpmailer' . DS);vendor(PHPMAILER_SUBDIR. 'class.phpmailer');

class EmailComponent extends Object

{

var $thtml;

var $layout='email';

var $to = null;

var $controller;

var $from = CONFIG_SMTP_EMAIL;

var $subject = null;

var $cc = null;

var $bcc = null;

var $formatoHTML=false; //false ->texto, true->html

var $charset="utf-8";

function startup(&$controller)

{

$this->controller =& $controller;

}

function send()

{

return $this->_sendmail($this->to, $this->subject, $this->_message());

}

/**

* Envia el email

* @param array $to Destinatarios. Cada elemento puede ser un dirección o un array nombre-dirección

* @param string $subject Asunto del mensaje

* @param string $mensaje El mensaje

* @param string $from Remitente.

*/

function _sendmail($to,$subject,$mensaje,$from='')

{

$mail = new PHPMailer();

$mail->PluginDir = VENDORS .PHPMAILER_SUBDIR ;

$mail->SetLanguage('en',VENDORS .PHPMAILER_SUBDIR);

$mail->CharSet= $this->charset;

$mail->IsSMTP();                                   // send via SMTP

$mail->Host     = CONFIG_SMTP_HOST; // SMTP servers

$mail->SMTPAuth = true;     // turn on SMTP authentication

$mail->Username = CONFIG_SMTP_USER;  // SMTP username

$mail->Password = CONFIG_SMTP_PASS; // SMTP password

if ($from =='') $mail->From = CONFIG_SMTP_EMAIL;

else $mail->From = $from;

$mail->FromName = '';

foreach ($to as $address)

{

$mail->AddAddress($address);

}

//$mail->WordWrap = 50;                              // set word wrap

//$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment

//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");

$mail->IsHTML($this->formatoHTML);                               // send as HTML

$mail->Subject  =  $subject;

$mail->Body     =  $mensaje;

//$mail->AltBody  =  "This is the text-only body";

return($mail->Send());

}

function _message()

{

ob_start();

$layout_backup=$this->controller->layout;

$this->controller->layout=$this->layout;

$this->controller->render($this->thtml);

$content= ob_get_clean();

$this->controller->layout=$layout_backup;

return $content;

}

}

?>

Don’t forget to edit the params at the top of the file

2. Download this class and put the files class.phpmailer.php class.smtp.php phpmailer.lang-en.php into /vendors/phpmailer

3. Include the email component

4. Example of usage.
In your action put:
<pre>
function myaction()
{
$this->set(‘data’,$this->params[‘data’]);//set $login variable
$this->email->controller=$this;
$this->email->thtml = ‘accept_email’;
$this->email->to = array($this->params[‘data’][‘Customer’][’email’]);
$this->email->subject = ‘hello !’;
return($this->email->send());
}
</pre>
Then create the file /app/views/mycontroller/accept_email.thtml , this is a normal view file and will be the body of your mail.

Comments»

1. Olegs - March 17, 2006

I did not read your blog,sorry 🙂 published this:
http://wiki.cakephp.org/tutorials:sending_email_with_phpmailer

2. rossoft - March 17, 2006

no problem, it’s your code

3. classifiedsgoa - September 25, 2008

Nice Script

4. murugesh - December 8, 2009

sdsds

5. Vigin Kurakar - October 5, 2010

if its working?

6. Vigin Kurakar - October 5, 2010

please give the code with html format also.


Leave a comment