Access-Control-Allow-Origin: *

joomlaコンタクトフォームに添付ファイルを実装する

まずテンプレートのオーバーライドを作成する

1.
joomla/templates/ja_purity_ii/html/com_contact/contact/default_form.php
または
joomla/templates/protostar/html/com_contact/contact/default_form.php
下記赤字部分を挿入
<div class="contact-form">
<form id="contact-form" enctype="multipart/form-data" action="<?php echo JRoute::_('index.php'); ?>" method="post" class="form-validate form-horizontal">
<fieldset>
****************
<!-- ここから -->
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_phone'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_phone'); ?></div>
</div>
<!-- ここまで -->
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_subject'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_subject'); ?></div>
</div>
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('contact_message'); ?></div>
<div class="controls"><?php echo $this->form->getInput('contact_message'); ?></div>
</div>
<!-- ここから -->
<div class="control-group">
<div class="control-label" style = "font-size:small;">添付ファイル1 500KBまで</div>
<input type="file" name="contact_image0" /><br/>
<div class="control-label" style = "font-size:small;">添付ファイル2 500KBまで</div>
<input type="file" name="contact_image1" /><br/>
<div class="control-label" style = "font-size:small;">添付ファイル3 500KBまで</div>
<input type="file" name="contact_image2" /><br/>
<div class="control-label" style = "font-size:small;">添付ファイル4 500KBまで</div>
<input type="file" name="contact_image3" /><br/>
</div>
<!-- ここまで -->

2.
joomla/templates/ja_purity_ii/html/com_contact/contact/default.php
または
joomla/templates/protostar/html/com_contact/contact/default.php
下記赤字部分を挿入
<?php
/* セッションを作成 */
session_cache_limiter(’none’);
session_start();
/**
* @package Joomla.Site
* @subpackage com_contact
******************************
<?php if ($this->params->get('show_contact_category') == 'show_with_link') : ?>
<?php $contactLink = ContactHelperRoute::getCategoryRoute($this->contact->catid); ?>
<h3>
<span class="contact-category"><a href="/<?php echo $contactLink; ?>">
<?php echo $this->escape($this->contact->category_title); ?></a>
</span>
</h3>
<?php endif; ?>

<!--ここから-->
<?php $category_ = $this->contact->category_title; ?>
<?php $_SESSION["category_"]=$this->contact->category_title; ?>
<!--ここまで-->

<?php if ($this->params->get('show_contact_list') && count($this->contacts) > 1) : ?>
<form action="#" method="get" name="selectForm" id="selectForm">
<?php echo JText::_('COM_CONTACT_SELECT_CONTACT'); ?>
<?php echo JHtml::_('select.genericlist', $this->contacts, 'id', 'class="inputbox" onchange="document.location.href = this.value"', 'link', 'name', $this->contact->link);?>
</form>
<?php endif; ?>

<?php if ($this->params->get('show_tags', 1) && !empty($this->item->tags)) : ?>
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
<?php endif; ?>

<?php if ($this->params->get('presentation_style') == 'sliders') : ?>
<?php echo JHtml::_('bootstrap.startAccordion', 'slide-contact', array('active' => 'basic-details')); ?>
<?php endif; ?>
<?php if ($this->params->get('presentation_style') == 'tabs') : ?>
<?php echo JHtml::_('bootstrap.startTabSet', 'myTab', array('active' => 'basic-details')); ?>
<?php endif; ?>

<?php if ($this->params->get('presentation_style') == 'sliders') : ?>
<?php echo JHtml::_('bootstrap.addSlide', 'slide-contact', JText::_('COM_CONTACT_DETAILS'), 'basic-details'); ?>
<?php endif; ?>
<?php if ($this->params->get('presentation_style') == 'tabs') : ?>
<?php echo JHtml::_('bootstrap.addTab', 'myTab', 'basic-details', JText::_('COM_CONTACT_DETAILS', true)); ?>
<?php endif; ?>
<?php if ($this->params->get('presentation_style') == 'plain'):?>
<!--2014.07.27-->
<?php echo '<h3>'. JText::_('COM_CONTACT_DETAILS'). " : " . "<span style = 'font-style: italic;color:#696969'>八ヶ岳スタイル " .$category_ . '担当</span></h3>'; ?>
<?php endif; ?>

************************

3.
joomla/components/com_contact/models/forms/contact.xml
下記赤字部分を挿入
<field name="contact_email"
type="email"
id="contact-email"
size="30"
description="COM_CONTACT_EMAIL_DESC"
label="COM_CONTACT_EMAIL_LABEL"
filter="string"
validate="contactemail"
required="true"
/>

<field name="contact_phone"
type="phone"
id="contact-phone"
size="30"
description="COM_CONTACT_PHONE_DESC"
label="COM_CONTACT_PHONE_LABEL"
filter="string"
/>

<field name="contact_subject"
type="text"
id="contact-emailmsg"
size="60"
description="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_DESC"
label="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_LABEL"
filter="string"
validate="contactemailsubject"
required="true"
/>
<field name="contact_message"
type="textarea"
cols="50"
rows="10"
id="contact-message"
description="COM_CONTACT_CONTACT_ENTER_MESSAGE_DESC"
label="COM_CONTACT_CONTACT_ENTER_MESSAGE_LABEL"
filter="safehtml"
validate="contactemailmessage"
required="true"
/>

<field name="contact_image0"
type="file"
id="contact-image0"
description="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_DESC"
label="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_LABEL"
/>
<field name="contact_image1"
type="file"
id="contact-image1"
size="100"
description="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_DESC"
label="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_LABEL"
/>
<field name="contact_image2"
type="file"
id="contact-image2"
size="100"
description="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_DESC"
label="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_LABEL"
/>
<field name="contact_image3"
type="file"
id="contact-image3"
size="100"
description="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_DESC"
label="COM_CONTACT_CONTACT_MESSAGE_SUBJECT_LABEL"
/>

<field name="contact_email_copy"
type="checkbox"
id="contact-email-copy"
description="COM_CONTACT_CONTACT_EMAIL_A_COPY_DESC"
label="COM_CONTACT_CONTACT_EMAIL_A_COPY_LABEL"
default="0"
/>
</fieldset>

4.joomla/components/com_contact/controllers/contact.php

<?php
/* セッションを作成 */
session_cache_limiter(’none’);
session_start();
/**
* @package Joomla.Site
* @subpackage com_contact
***************
$name = $data['contact_name'];
$email = JStringPunycode::emailToPunycode($data['contact_email']);


$phone = $data['contact_phone'];

$subject = $data['contact_subject'];

$form_title = $_SESSION["form_title"];
$category_ = $_SESSION["category_"];

$body = "「" . $category_ . "担当」へメールが届きました。\r\n\r\n" . "電話番号 : " . $phone . "\r\n" . $data['contact_message'];

$uploads_dir = '/tmp/';
for ($i = 0; $i < 4; $i++) {
$tmp_name = $_FILES["contact_image" . $i]["tmp_name"];
$uploadfile = $uploads_dir . $_FILES["contact_image" . $i]["name"];
$upfile[$i] = $uploadfile;
move_uploaded_file($tmp_name, $uploadfile);
}

// Prepare email body
$prefix = JText::sprintf('COM_CONTACT_ENQUIRY_TEXT', JUri::base());
$body = $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);

$mail = JFactory::getMailer();
$mail->addRecipient($contact->email_to);
$mail->addReplyTo(array($email, $name));
$mail->setSender(array($mailfrom, $fromname));
$mail->setSubject($sitename.': '.$subject);
$mail->setBody($body);

$mail->addAttachment($upfile[0]);
$mail->addAttachment($upfile[1]);
$mail->addAttachment($upfile[2]);
$mail->addAttachment($upfile[3]);

$sent = $mail->Send();

***********************
参考:https://www.youtube.com/watch?v=uQzb7vfZSJI

こんな感じ