<?php
function array_to_csv_string($array, $filename = "3dHouse.csv", $delimiter=",") {
    //header('Content-Type: application/csv');
    //header('Content-Disposition: attachment; filename="'.$filename.'";');

    // open the "output" stream
    // see http://www.php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
    //$f = fopen('php://output', 'w');
    // Open temp file pointer
	if (!$f = fopen('php://temp', 'w+')) return FALSE;

    foreach ($array as $line) {
        fputcsv($f, explode(',',$line));
    }
    // Place stream pointer at beginning
    rewind($f);

    // Return the data
    return stream_get_contents($f);
}
if( $_POST ){
	$adminEmail = 'kitayama@main.co.jp';
    $fullname = $_POST['txtName'];
    $email = $_POST['txtEmail'];
    $location = $_POST['optLocate'];
    $phone = $_POST['txtMobile'];
    $address = $_POST['txtAddress'];
    $requestDate = $_POST['txtRequestDate'];
    $note = $_POST['txtNote'];
    // $data = json_decode($_POST['data'], true);
	$data = $_POST['csv-data'];
	$arrayData = explode('|',$data);
	 
	$csvAttachString = array_to_csv_string($arrayData, "3dHouse.csv");

	

 //    $htmlInfo = '<table class="table table-striped" border="0">
    
 //    <tr>
 //    <td colspan="2">
 //    	<div class="alert alert-info">
 //    		<strong>Success</strong>, Your request have been send Successfully...
 //    	</div>
 //    </td>
 //    </tr>';
 //    //Full Name
 //    $htmlInfo .= '<tr><td>氏名</td><td>';
 //    $htmlInfo .= $fullname;
	// $htmlInfo .= '</td></tr>';

	// //Email
	// $htmlInfo .= '<tr><td>メールアドレス</td><td>';
 //    $htmlInfo .= $email;
 //    $htmlInfo .= '</td></tr>';
    
 //    // Location
 //    $htmlInfo .= '<tr><td>配送先</td><td>';
 //    $htmlInfo .= $location;
	// $htmlInfo .= '</td></tr>';
    
 //    // Phone
 //    $htmlInfo .= '<tr><td>電話番号</td><td>';
 //    $htmlInfo .= $phone;
	// $htmlInfo .= '</td></tr>';

	// // address
 //    $htmlInfo .= '<tr><td>住所</td><td>';
 //    $htmlInfo .= $address;
	// $htmlInfo .= '</td></tr>';

	// // request date
 //    $htmlInfo .= '<tr><td>希望納期</td><td>';
 //    $htmlInfo .= $requestDate;
	// $htmlInfo .= '</td></tr>';

	// // note
 //    $htmlInfo .= '<tr><td>質問事項</td><td>';
 //    $htmlInfo .= $note;
	// $htmlInfo .= '</td></tr>';

 //    $htmlInfo .= '</table>';
 

 	$htmlInfo = '';
 	$htmlInfo .= '<p>【氏名】<br>';
 	$htmlInfo .= $fullname;
 	$htmlInfo .= '</p>';

 	$htmlInfo .= '<p>【メールアドレス】<br>';
 	$htmlInfo .= $email;
 	$htmlInfo .= '</p>';

 	$htmlInfo .= '<p>【配送先】<br>';
 	$htmlInfo .= $location;
 	$htmlInfo .= '</p>';

 	$htmlInfo .= '<p>【電話番号】<br>';
 	$htmlInfo .= $phone;
 	$htmlInfo .= '</p>';

 	$htmlInfo .= '<p>【住所】<br>';
 	$htmlInfo .= $address;
 	$htmlInfo .= '</p>';

 	$htmlInfo .= '<p>【希望納期】<br>';
 	$htmlInfo .= $requestDate;
 	$htmlInfo .= '</p>';

 	$htmlInfo .= '<p>【質問事項】<br>';
 	$htmlInfo .= $note;
 	$htmlInfo .= '</p>';

    $htmlMailContent ='';
	// Build HTML mail
	$htmlMailContent .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>3Dシミュレータからのお問合わせ</title>
	</head>
	<body>
	<div style="width: 640px; font-family: ms-gothic, sans-serif; font-size: 11px;">
		<h3>ホームページの3Dシミュレータから、正式見積の依頼がありました。</h3>
		<h4>以下、お客様の情報です。</h4>';
	$htmlMailContent .= $htmlInfo;
	$htmlMailContent .= '</div></body></html>';





    // Prepare to send email

	require 'PHPMailer/PHPMailerAutoload.php';
	//SMTP time zone set
	date_default_timezone_set('Etc/UTC');

	///
	///Send mail by using gmail smtp
	///
	//Create a new PHPMailer instance
	$mail = new PHPMailer;
	// To load the French version
	$mail->setLanguage('ja', 'PHPMailer/language/phpmailer.lang-ja.php');
	//Tell PHPMailer to use SMTP
	$mail->isSMTP();

	//Enable SMTP debugging
	// 0 = off (for production use)
	// 1 = client messages
	// 2 = client and server messages
	$mail->SMTPDebug = 0;
	//Ask for HTML-friendly debug output
	$mail->Debugoutput = 'html';
	//Set the hostname of the mail server
	$mail->Host = 'smtp.gmail.com';
	// use
	// $mail->Host = gethostbyname('smtp.gmail.com');
	// if your network does not support SMTP over IPv6
	//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
	$mail->Port = 587;
	//Set the encryption system to use - ssl (deprecated) or tls
	$mail->SMTPSecure = 'tls';
	//Whether to use SMTP authentication
	$mail->SMTPAuth = true;
	//Username to use for SMTP authentication - use full email address for gmail
	$mail->Username = "naitce@gmail.com";
	//Password to use for SMTP authentication
	$mail->Password = "Xinchaocacban";
	//Set who the message is to be sent from
	$mail->setFrom('noreply@timesfun.net', '3Dシミュレー');
	//Set an alternative reply-to address
	//$mail->addReplyTo('replyto@example.com', 'First Last');
	//Set who the message is to be sent to
	$mail->addAddress($adminEmail, 'Admin');
	$mail->isHTML(true);


	//Set the subject line
	$mail->Subject = '3Dシミュレータからのお問合わせ';
	//Read an HTML message body from an external file, convert referenced images to embedded,
	//convert HTML into a basic plain-text alternative body
	$mail->msgHTML($htmlMailContent);
	//Replace the plain text body with one created manually
	$mail->AltBody = 'ホームページの3Dシミュレータから、正式見積の依頼がありました。';
	$mail->CharSet="UTF-8";
	//Attach an image file
	$mail->AddStringAttachment($csvAttachString, '3dHouse.csv', 'base64', 'application/csv');
	 // Make the attachment
    //$attachment = chunk_split(base64_encode(create_csv_string($csvData))); 
	//$mail->addAttachment($output,'application/pdf','3DHouse-Estimation.pdf', false);
	//$mail->addAttachment('images/phpmailer_mini.png');
	//send the message, check for errors
	if (!$mail->send()) {
	    echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
	    echo $htmlInfo;
	}
	
}

?>