Reputation: 41
Is there any default functionality in Magento for a simple contact form that needs to send a file attachment? Or do I need to customise it with Zend's mail function? Any help or suggestions would be appreciated.
Upvotes: 4
Views: 6875
Reputation: 3797
Here is the post describing how easily you can send email with attachments:
Upvotes: 0
Reputation: 10022
Not Magento related, but I use PHP's SwiftMailer (http://swiftmailer.org/):
require_once('../lib/swiftMailer/lib/swift_required.php');
...
$body="Dear $fname,\n\nPlease find attached, an invoice for the period $startDate - $endDate\n\nBest regards,\n\nMr X";
$message = Swift_Message::newInstance('Subject goes here')
->setFrom(array($email => "[email protected]"))
->setTo(array($email => "$fname $lname"))
->setBody($body);
$message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf"));
$result = $mailer->send($message);
Upvotes: 0
Reputation: 8585
To avoid editing the phtml file, I like to use events:
create observer in config.xml:
<events>
<core_block_abstract_to_html_after>
<observers>
<add_file_boton>
<type>singleton</type>
<class>contactattachment/observer</class>
<method>addFileBoton</method>
</add_file_boton>
</observers>
</core_block_abstract_to_html_after>
</events>
the observer:
class Osdave_ContactAttachment_Model_Observer
{
public function addFileBoton($observer)
{
$block = $observer->getEvent()->getBlock();
$nameInLayout = $block->getNameInLayout();
if ($nameInLayout == 'contactForm') {
$transport = $observer->getEvent()->getTransport();
$block = Mage::app()->getLayout()->createBlock('contactattachment/field');
$block->setPassingTransport($transport['html']);
$block->setTemplate('contactattachment/field.phtml')
->toHtml();
}
return $this;
}
}
the block class (that you've just instanciated in the observer):
class Osdave_ContactAttachment_Block_Field extends Mage_Core_Block_Template
{
private $_passedTransportHtml;
/**
* adding file select field to contact-form
* @param type $transport
*/
public function setPassingTransport($transport)
{
$this->_passedTransportHtml = $transport;
}
public function getPassedTransport()
{
return $this->_passedTransportHtml;
}
}
the .phtml file, where you add the enctype attribute to the form and add the file input:
<?php
$originalForm = $this->getPassedTransport();
$originalForm = str_replace('action', 'enctype="multipart/form-data" action', $originalForm);
$lastListItem = strrpos($originalForm, '</li>') + 5;
echo substr($originalForm, 0, $lastListItem);
?>
<li>
<label for="attachment"><?php echo $this->__('Select an attachment:') ?></label>
<div class="input-box">
<input type="file" class="input-text" id="attachment" name="attachment" />
</div>
</li>
<?php
echo substr($originalForm, $lastListItem);
?>
you need to rewrite Magento's Contacts IndexController to upload the file where you want to and add the link in the email.
config.xml:
<global>
...
<rewrite>
<osdave_contactattachment_contact_index>
<from><![CDATA[#^/contacts/index/#]]></from>
<to>/contactattachment/contacts_index/</to>
</osdave_contactattachment_contact_index>
</rewrite>
...
</global>
<frontend>
...
<routers>
<contactattachment>
<use>standard</use>
<args>
<module>Osdave_ContactAttachment</module>
<frontName>contactattachment</frontName>
</args>
</contactattachment>
</routers>
...
</frontend>
the controller:
<?php
/**
* IndexController
*
* @author david
*/
require_once 'Mage/Contacts/controllers/IndexController.php';
class Osdave_ContactAttachment_Contacts_IndexController extends Mage_Contacts_IndexController
{
public function postAction()
{
$post = $this->getRequest()->getPost();
if ( $post ) {
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);
$error = false;
if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
$error = true;
}
if ($error) {
throw new Exception();
}
//upload attachment
try {
$uploader = new Mage_Core_Model_File_Uploader('attachment');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(true);
$uploader->setAllowCreateFolders(true);
$result = $uploader->save(
Mage::getBaseDir('media') . DS . 'contact_attachments' . DS
);
$fileUrl = str_replace(Mage::getBaseDir('media') . DS, Mage::getBaseUrl('media'), $result['path']);
} catch (Exception $e) {
Mage::getSingleton('customer/session')->addError(Mage::helper('contactattachment')->__('There has been a problem with the file upload'));
$this->_redirect('*/*/');
return;
}
$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array('data' => $postObject)
);
if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
$this->_redirect('*/*/');
return;
}
} else {
$this->_redirect('*/*/');
}
}
}
in the controller you need to add the $fileUrl
into the email template, and on your email template file you need to echo it.
I think this is the whole thing, let me know if you're having troubles with that.
cheers
Upvotes: 3
Reputation: 1478
For adding an attachment to an email you will need to use the following Zend_Mail function:
public function createAttachment($body,
$mimeType = Zend_Mime::TYPE_OCTETSTREAM,
$disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
$encoding = Zend_Mime::ENCODING_BASE64,
$filename = null)
{
Here is an example to use it with Magento, for attaching a pdf file to the email:
$myEmail = new new Zend_Mail('utf-8'); //see app/code/core/Mage/Core/Model/Email/Template.php - getMail()
$myEmail->createAttachment($file,'application/pdf',Zend_Mime::DISPOSITION_ATTACHMENT,Zend_Mime::ENCODING_BASE64,$name.'.pdf');
You can use this extension for inspiration: Fooman Email Attachments
Upvotes: 2
Reputation: 1791
Not so sure about default functionality but this may help - http://ecommercesoftwaresolutionsonline.com/magento-enquiry-feedback-form-with-attachment-extension.html
Upvotes: 0