I created a simple contact form and now trying to add feature where user can attach a file and sent it via email.
I have added the upload field to my .phtml
file but I am not sure how to attach the file and sent it. Can anyone help me a bit, this is the last step but a bit too much for me...
Here is what I have so far:
app/etc/modules/Recruitment_SimpleContact.xml
<?xml version="1.0"?>
<config>
<modules>
<Recruitment_SimpleContact>
<active>true</active>
<codePool>local</codePool>
</Recruitment_SimpleContact>
</modules>
</config>
app/code/local/Recruitment/SimpleContact/controllers/IndexController.php
<?php
class Recruitment_SimpleContact_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
//Get current layout state
$this->loadLayout();
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'recruitment.simple_contact',
array(
'template' => 'recruitment/simple_contact.phtml'
)
);
$this->getLayout()->getBlock('content')->append($block);
//$this->getLayout()->getBlock('right')->insert($block, 'catalog.compare.sidebar', true);
$this->_initLayoutMessages('core/session');
$this->renderLayout();
}
public function sendemailAction() {
$params = $this->getRequest()->getParams();
$name = $this->getRequest()->getParam('name');
$email = $this->getRequest()->getParam('email');
$jobRole = $this->getRequest()->getParam('job_role');
$telephoneNumber = $this->getRequest()->getParam('telephone_number');
$comment = $this->getRequest()->getParam('comment');
$body1 = "Name: " .$name ."\r\n";
$body2 = "Job Role " .$jobRole ."\r\n";
$body3 = "Telephone: " .$telephoneNumber ."\r\n";
$body4 = "Additional Comments: " .$comment ."\r\n";
$body = $body1.' '.$body2.' '.$body3.' '.$body4.' ' ;
$mail = new Zend_Mail();
$mail->setBodyText($body);
$mail->setFrom($email, $name);
$mail->addTo('[email protected]');
$mail->setSubject('Recruitment enquiries');
try {
$mail->send();
}
catch(Exception $ex) {
Mage::getSingleton('core/session')->addError('Unable to send email. Please check that the details you have entered are correct.');
}
//Redirect back to index action of (this) recruitment-simplecontact controller
$this->_redirect('recruitment-simplecontact/');
}
}
?>
app/code/local/Recruitment/SimpleContact/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Recruitment_SimpleContact>
<version>0.1.0</version>
</Recruitment_SimpleContact>
</modules>
<frontend>
<routers>
<RandomRouter2>
<use>standard</use>
<args>
<module>Recruitment_SimpleContact</module>
<frontName>recruitment-simplecontact</frontName>
</args>
</RandomRouter2>
</routers>
</frontend>
</config>
app/design/frontend/default/default/template/recruitment/simple_contact.phtml
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>
<div class="page-title">
<h1><?php echo Mage::helper('contacts')->__('Recruitment Contact') ?></h1>
</div>
<form id="contactForm" name="simple_contact_form" action="<?php echo $this->getUrl('recruitment-simplecontact/') ?>index/sendemail" method="post" enctype="multipart/form-data">
<div class="fieldset">
<fieldset class="group-select">
<h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
<ul>
<li class="fields">
<div class="field">
<label for="name">Name <span class="required"><em>*</em></span></label><br />
<input name="name" id="name" title="Name" value="" class="required-entry input-text" type="text" />
</div>
<div class="input-box">
<label for="email">Email <span class="required"><em>*</em></span></label><br />
<input name="email" id="email" title="Email" value="" class="required-entry input-text validate-email" type="text" />
</div>
<div class="input-box">
<label for="job_role">Job role<span class="required"><em>*</em></span></label><br />
<input name="job_role" id="job_role" title="Job Role" value="" class="required-entry input-text" type="text" />
</div>
<div class="input-box">
<label for="telephone_number">Telephone Number <span class="required"><em>*</em></span></label><br />
<input name="telephone_number" id="telephone_number" title="Telephone Number" value="" class="required-entry input-text" type="text" />
</div>
<div class="clear"></div>
<div class="input-box">
<label for="comment">Comment</label><br />
<textarea name="comment" id="comment" title="Comment" class="required-entry input-text" style="height:100px;" cols="50" rows="3"></textarea>
</div>
<div class="input-box">
<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="uploaded_file">
</div>
</li>
</ul>
</fieldset>
</div>
<div class="button-set">
<p class="required">* Required Fields</p>
<button style="float: right; margin-bottom: 10px;" class="button" type="submit"><span>Submit <br></span></button>
<p></p>
</div>
</form>
All I need is a simple file attachment to this form, seems straight forward but just exceeds my capability.
Can someone help me add a file attachment to this form and sent via email?