AydEmail Component for CakePHP

Summary

This is a component to send email from CakePHP using PHPMailer, based on: http://wiki.cakephp.org/tutorials:sending_email_with_phpmailer http://wiki.cakephp.org/tutorials:sending_email

Download it

Download the newest version from our svn:

svn://devayd.com/cakephp/components/ayd_email/trunk

Installation

1. Download PHPMailer from sourceforge http://sourceforge.net/projects/phpmailer/

and place it in /app/vendors/phpmailer

2. Download the component from our svn server and copy it to /app/controllers/components

Configuration

Config file

you can add this default values to your app/config/core.php file

the component will detect it and setup these values automatically.

app/config/core.php

define('AYD_EMAIL_FROM', 'your@emailfrom.com');
define('AYD_EMAIL_FROM_NAME', 'From Name'); 
define('AYD_EMAIL_TO', 'to@name.com');
define('AYD_EMAIL_TO_NAME', null);
		 
define('AYD_EMAIL_IS_SMTP', false);
define('AYD_EMAIL_SMTP_USER_NAME', null);
define('AYD_EMAIL_SMTP_PASSWORD', null);
define('AYD_EMAIL_SMTP_HOST_NAMES', null);
		 
define('AYD_EMAIL_CHARSET', 'UTF-8');

Email templates

Your email templates should be stored in app/views/ayd_email/

for ex. contactconfirmation.thtml or contactconfirmation_html.thtml

Controller

And finally in order to use this component you must add it to the components array in your controller

var $components = array("AydEmail");

Component

You can also set additional options in the component file. Please check the source on how to do it.

Examples

Contact form

This is the easiest way to use this component. It works very well for contact forms, where you have to send an email to the admin and a confirmation to the user.

Html Form

app/views/pages/contact

<form action="/pages/contact" method="post" enctype="application/x-www-form-urlencoded" >
	<div class="formRow required">
		<span class="formLabel">
			<?php echo $form->labelTag('Email/fromName', 'Su Nombre');?>
		</span>
		<span class="formField required">
			<?php echo $html->input('Email/fromName', array('size' => '60'));?>
		</span>                                        
	</div>
	<div class="formRow required">
		<span class="formLabel">                                  
			<?php echo $form->labelTag('Email/from', 'Su Email');?>
		</span>
		<span class="formField">
			<?php echo $html->input('Email/from', array('size' => '60'));?>
		</span>
	</div>
	<div class="formRow required">
		<span class="formLabel">
			<?php echo $form->labelTag('Email/subject', 'Asunto');?>
		</span>
		<span class="formField">
			<?php echo $html->input('Email/subject', array('size' => '60'));?>
		</span>
	</div>
	<div class="formRow">
		<span class="formLabel">
			<?php echo $form->labelTag('Email/body', 'Mensaje');?>
		</span>
		<span class="formField">
			<?php echo $html->textarea('Email/body', array('cols' => '57', 'rows' => '10'));?>
		</span>
	</div>
	<div class="formRow">
		<?php echo $html->hidden('Email/redirect_on_success', array('value' => '/pages/contakt_ok'));?>
		<input type="submit" value="Enviar" id="enviar" name="enviar" class="buttonSubmit" />
	</div>
</form>

Thank You page

This page will be shown on success. (you can configure it setting a hidden field ‘Email/redirect_on_success’)

/app/views/pages/contact_ok.thtml

<h2>Thank You!</h2>
<p>Your message has been sent correctly.
</p>

Email templates

create these templates in app/views/ayd_email directory

contact_admin.thtml (this is a notification for the admin)

This email has been sent from:
<?php echo $mailer_pageURL ?> 
on: <?php echo $mailer_date ?> 
 
 
Form Data:
-------------------------------------------------
Name: <?php echo $contact['fromName'] ?> 
Email: <?php echo $contact['from'] ?> 
 
Message:
-------------------------------------------------
<?php echo $contact['body'] ?> 
-------------------------------------------------

contact_user.thtml

Dear <?php echo $contact['fromName'] ?>
 
Thank you for your message.
Best regards

Controller

in your /app/controllers/pages_controller.php

add this:

function contact()
{
 if (!empty($this->data))
 {
  $this->set('contact', $this->data['Email']);
  
  $this->AydEmail->sendToUserAndAdmin('contact', $this->data['Email']);
		
  }
}

the line

 $this->set('contact', $this->data['Email']);

makes user data available for the templates.

and this one:

$this->AydEmail->sendToUserAndAdmin('contact', $this->data['Email']);
  1. sets some variables like from, fromName, subject using $this→data[’Email’] array
  2. sends an email to the admin, defined in: AYD_EMAIL_TO constant, using contact_admin template
  3. sends an email to the user, using $this→data[’Email’][’from’] variable and contact_user template
  4. redirects the user to the /pages/contakt_ok (as defined in $this→data[’Email’][’redirect_on_success’])

License

This code is licensed under the LGPL license.

shortly: you can use this library in your projects (whatever license you choose for them), but you have to maintain the original license for this library and republish any changes you make to it. you can send them back to us and we will return it to the community.

please check the license text if you have any doubts.

Support

Please send your bug reports and improvements to: http://flyspray.devayd.com/


Personal Tools