<?php

//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include './conn.php';


if (!empty($_POST["email"]) && $_POST["email"] != '') {


    $salutation = $_POST['salutation'];
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $note = $_POST['note'];


    $result = mysql_query("SELECT smtp_server,smtp_user,smtp_pass,smtp_email,sitename,email_footer,session_time,sms_user,sms_password,sms_sys_zum,sms_sys_vom,sms_sys_zum_f,sms_sys_vom_f,sms_web_zum,sms_web_vom,child_seat_price,booster_seat_price,carrycot_price,additional_address_price,map_in_forms,invoice_year,sys_path,sys_port,night_price_wien,night_price_other,price_balance,creditcard_price,icons_in_form,email_trip_info,company_phone,company_url,company_atu,company_bankname,company_iban,company_konto,company_bic,company_blz,rechnung_h1,rechnung_header,company_admin,sms_send_name,sms_number_bottom,invoice_days,from_airport_plus,from_to_minus,backup_fahrten_email,order_number_prefix FROM options WHERE id = '1'");
    if ($result === FALSE) {
	die(mysql_error()); // TODO: better error handling
    }
    $row = mysql_fetch_array($result);

    $opt_smtp_server = $row['smtp_server'];
    $opt_smtp_user = $row['smtp_user'];
    $opt_smtp_pass = $row['smtp_pass'];
    $opt_smtp_email = $row['smtp_email'];
    $opt_sitename = $row['sitename'];
    $opt_email_footer = $row['email_footer'];
    $opt_session_time = $row['session_time'];
    $opt_sms_user = $row['sms_user'];
    $opt_sms_password = $row['sms_password'];
    $opt_sms_sys_zum = $row['sms_sys_zum'];
    $opt_sms_sys_vom = $row['sms_sys_vom'];
    $opt_sms_sys_zum_f = $row['sms_sys_zum_f'];
    $opt_sms_sys_vom_f = $row['sms_sys_vom_f'];
    $opt_sms_web_zum = $row['sms_web_zum'];
    $opt_sms_web_vom = $row['sms_web_vom'];
    $opt_child_seat_price = $row['child_seat_price'];
    $opt_booster_seat_price = $row['booster_seat_price'];
    $opt_carrycot_price = $row['carrycot_price'];
    $opt_additional_address_price = $row['additional_address_price'];
    $opt_map_in_forms = $row['map_in_forms'];
    $opt_invoice_year = $row['invoice_year'];
    $opt_sys_path = $row['sys_path'];
    $opt_sys_port = $row['sys_port'];
    $opt_night_price_wien = $row['night_price_wien'];
    $opt_night_price_other = $row['night_price_other'];
    $opt_price_balance = $row['price_balance'];
    $opt_creditcard_price = $row['creditcard_price'];
    $opt_icons_in_form = $row['icons_in_form'];
    $opt_email_trip_info = $row['email_trip_info'];
    $opt_company_phone = $row['company_phone'];
    $opt_company_url = $row['company_url'];
    $opt_company_atu = $row['company_atu'];
    $opt_company_bankname = $row['company_bankname'];
    $opt_company_iban = $row['company_iban'];
    $opt_company_konto = $row['company_konto'];
    $opt_company_bic = $row['company_bic'];
    $opt_company_blz = $row['company_blz'];
    $opt_rechnung_h1 = $row['rechnung_h1'];
    $opt_rechnung_header = $row['rechnung_header'];
    $opt_company_admin = $row['company_admin'];
    $opt_sms_send_name = $row['sms_send_name'];
    $opt_sms_number_bottom = $row['sms_number_bottom'];
    $opt_invoice_days = $row['invoice_days'];
    $opt_from_airport_plus = $row['from_airport_plus'];
    $opt_from_to_minus = $row['from_to_minus'];
    $backup_fahrten_email = $row['backup_fahrten_email'];
    $order_number_prefix = $row['order_number_prefix'];













    $to = $email; // note the comma

    $subject = 'Webseite Kontakt von ' . $salutation . ' ' . $name . ' ' . $surname;

    $message = ' <!DOCTYPE html>
<html>
    <head>
        <title>Fahrt</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>


<table style="border-collapse: collapse;" border="1" width="500px;">
<tbody>
<tr>
<th>Webseite Kontakt</th>
<th></th>
</tr>
<tr>
<td style="padding: 5px;padding-left:30px;">Name</td>
<td style="padding: 5px;">' . $salutation . ' ' . $name . ' ' . $surname . '</td>
</tr>
<tr>
<td style="padding: 5px;padding-left:30px;">E-Mail</td>
<td style="padding: 5px;">' . $email . '</td>
</tr>
<tr>
<td style="padding: 5px;padding-left:30px;">Telefonnummer</td>
<td style="padding: 5px;">' . $phone . '</td>
</tr>
<tr>
<td style="padding: 5px;padding-left:30px;">Nachricht</td>
<td style="padding: 5px;">' . $note . '</td>
</tr>
<tr>

</tbody>
</table>


</body>
</html>
';


    require './imapMail/PHPMailerAutoload.php';
    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP

    $mail->SMTPDebug = 0;
    // 0 = no output, 1 = errors and messages, 2 = messages only.

    $mail->SMTPAuth = true;  // enable SMTP authentication
    $mail->addReplyTo($to, $name . ' ' . $surname);
    $mail->SMTPSecure = "tls";       // sets the prefix to the servier
    $mail->Host = $opt_smtp_server; // sets Gmail as the SMTP server
    $mail->Port = 587;       // set the SMTP port for the GMAIL

    $mail->Username = $opt_smtp_user;  // Gmail username
    $mail->Password = $opt_smtp_pass;      // Gmail password

    $mail->CharSet = 'UTF-8';
    $mail->SetFrom($opt_smtp_email, $opt_sitename);
    $mail->Subject = $subject;
    $mail->ContentType = 'text/html';
    $mail->IsHTML(true);

    $mail->Body = $message;
    // you may also use $mail->Body = file_get_contents('your_mail_template.html');

    $mail->AddAddress($opt_smtp_email, $opt_sitename);

    // you may also use this format $mail->AddAddress ($recipient);

    if (!$mail->Send()) {
	$emailsent = 'ERROR';
    } else {
	$emailsent = 'OK';
    }

    echo $emailsent;




    //echo $success;
} else {
    echo 'ERROR';
}
?>