Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

.docx attachment not working in custom contact form


Reply

6 Posts   3348 Views

Avatar
lozhowlett

2 December 2011 at 11:37pm Community Member, 146 Posts

Hi everyone,

I am trying to make a CV upload tool...

function UploadCV() {
// Create fields
$fields = new FieldSet(
new TextField('Name', 'Name'),
new EmailField('Email', 'Email'),
new TextField('Telephone','Telephone / Mobile'),
new FileField('CVUpload', 'CV Attachment')

);

// Create action
$actions = new FieldSet(
new FormAction('SendContactForm', 'UPLOAD & SEND')
);

// Create Validators
$validator = new RequiredFields('Name', 'Email', 'Telephone');

return new Form($this, 'UploadCV', $fields, $actions, $validator);
}

function SendContactForm($data, $form) {

//Set data
$From = $data['Email'];
$To = "lawrence@newedge.co.uk";
$Subject = "CV Upload Submission";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('CvUpload');
//populate template
$email->populateTemplate($data);
//attach upload
if (isset($_FILES["CVUpload"]) && is_uploaded_file($_FILES["CVUpload"]["tmp_name"])) {
$email->attachFile($_FILES["CVUpload"]["tmp_name"], $_FILES["CVUpload"]["name"]);
}
//send mail
$email->send();
//return to submitted message
Director::redirect($this->Link("?success=1"));
}

public function Success()
{
return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
}

However when I select a .docx file I get this error....

[Notice] Undefined index: docx
POST /candidates/UploadCV

Line 451 in /var/sites/s/seimansears.co.uk/public_html/sapphire/email/Mailer.php

Source

442 }
443
444 /*
445 * Get mime type based on extension
446 */
447 function getMimeType($filename) {
448    global $global_mimetypes;
449    if(!$global_mimetypes) loadMimeTypes();
450    $ext = strtolower(substr($filename,strrpos($filename,'.')+1));
451    return $global_mimetypes[$ext];
452 }
453
454 /*
455 * Load the mime-type data from the system file
456 */
457 function loadMimeTypes() {
Trace

getMimeType(ecommerce questions.docx)
Line 389 of Mailer.php
encodeFileForEmail(Array)
Line 116 of Mailer.php
htmlEmail(lawrence@newedge.co.uk,lawrence@newedge.co.uk,CV Upload Submission,<html> <body> <p>The following message was submitted to your site by <a href="mailto:lawrence@newedge.co.uk">lawrence howlett:</a></p> <p>Name: lawrence howlett</P> <p>Email: lawrence@newedge.co.uk</P> <p>Telephone / Mobile: 448707606812</P> </body> </html>,Array,Array,,)
Line 31 of Mailer.php
Mailer->sendHTML(lawrence@newedge.co.uk,lawrence@newedge.co.uk,CV Upload Submission,<html> <body> <p>The following message was submitted to your site by <a href="mailto:lawrence@newedge.co.uk">lawrence howlett:</a></p> <p>Name: lawrence howlett</P> <p>Email: lawrence@newedge.co.uk</P> <p>Telephone / Mobile: 448707606812</P> </body> </html>,Array,Array,)
Line 486 of Email.php
Email->send()
Line 111 of Page.php
Page_Controller->SendContactForm(Array,Form,SS_HTTPRequest)
Line 329 of Form.php
Form->httpSubmission(SS_HTTPRequest)
Line 143 of RequestHandler.php
RequestHandler->handleRequest(SS_HTTPRequest)
Line 161 of RequestHandler.php
RequestHandler->handleRequest(SS_HTTPRequest)
Line 147 of Controller.php
Controller->handleRequest(SS_HTTPRequest)
Line 199 of ContentController.php
ContentController->handleRequest(SS_HTTPRequest)
Line 67 of ModelAsController.php
ModelAsController->handleRequest(SS_HTTPRequest)
Line 282 of Director.php
Director::handleRequest(SS_HTTPRequest,Session)
Line 125 of Director.php
Director::direct(/candidates/UploadCV)
Line 127 of main.php

I have done some searching around and cant find any answers... does anyone know how to fix this issue?

Thanks!

Avatar
lozhowlett

2 December 2011 at 11:38pm Community Member, 146 Posts

Ps, it works fine with .doc, .pdf, .jpg. Thanks,

Avatar
swaiba

2 December 2011 at 11:48pm Forum Moderator, 1796 Posts

that is because when using attachFile you are not specficy the 3rd argument of the mime type and silverstripe doesn't have access to an entry for docx

$ext = //get file extenstion
if ($ext == 'docx') {
   $filetype = 'application/msword';
}
else {
   $filetype = HTTP::getMimeType($filename);
}
$email->attachFile($filename,$filename, $filetype);

Avatar
avengex

14 December 2011 at 12:37am (Last edited: 14 December 2011 12:37am), Community Member, 11 Posts

Actually the true source of the problem, that can be fixed without any hacks to the core, is by updating your /etc/mime.types file.

If you open that you'll probably see:

application/msword doc

You just need to add docx next to doc, like so:

application/msword doc docx

Then restart Apache.

Avatar
lozhowlett

24 September 2012 at 8:10pm Community Member, 146 Posts

HI everyone

A few months later and I am now getting this error for PDFs??

I have tried

if ($ext == 'docx') {
$filetype = 'application/msword';
}
else if ($ext == 'pdf') {
$filetype = 'application/pdf';
} else {
$filetype = HTTP::getMimeType($_FILES["CVAttchment"]["tmp_name"]);
}

But its not working?

Notice] Undefined index: pdf
POST /job-details/ContactForm

Line 451 in /var/sites/p/pytec.co.uk/public_html/sapphire/email/Mailer.php

Trace

getMimeType(TF_logo.pdf)
Line 389 of Mailer.php
encodeFileForEmail(Array)
Line 116 of Mailer.php

Avatar
lozhowlett

24 October 2012 at 8:22pm Community Member, 146 Posts

*bump