Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » .docx attachment not working in custom contact form

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

Page: 1
Go to End
Author Topic: 3053 Views
  • lozhowlett
    Avatar
    Community Member
    140 Posts

    .docx attachment not working in custom contact form Link to this post

    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!

  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Re: .docx attachment not working in custom contact form Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: .docx attachment not working in custom contact form Link to this post

    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);

  • avengex
    Avatar
    Community Member
    11 Posts

    Re: .docx attachment not working in custom contact form Link to this post

    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.

  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Re: .docx attachment not working in custom contact form Link to this post

    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

  • lozhowlett
    Avatar
    Community Member
    140 Posts
    3053 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.