P
P
pesterevilya2016-05-05 14:55:52
PHP
pesterevilya, 2016-05-05 14:55:52

How to set up phpmailer to send data from a complex form?

I can’t figure out how to set up the form so that the data from the select comes to the mail (several values ​​\u200b\u200bcan be selected there), as well as attached files, which can also be several.
My form code in HTML:

<form class="nobottommargin" id="template-contactform" name="template-contactform" action="include/sendemail.php" method="post">
 
  <div class="form-process"></div>
  <h4>Куда оптравить наше предложение?</h4>
  <div class="col_one_third">
    <label for="template-contactform-name">Имя <small>*</small>
    </label>
    <input type="text" id="template-contactform-name" name="template-contactform-name" value="" class="sm-form-control required" />
  </div>
 
  <div class="col_one_third">
    <label for="template-contactform-phone">Телефон <small>*</small>
    </label>
    <input type="text" id="template-contactform-phone" name="template-contactform-phone" value="" class="sm-form-control required" />
  </div>
 
  <div class="col_one_third col_last">
    <label for="template-contactform-email">E-mail <small>*</small>
    </label>
    <input type="email" id="template-contactform-email" name="template-contactform-email" value="" class="required email sm-form-control" />
  </div>
 
  <div class="clear"></div>
 
  <div class="col_full">
    <label>Какие материалы вы используете?</label>
    <select id="template-contactform-service" name="template-contactform-service" class="sm-form-control selectpicker" multiple title="Кликните для выбора">
      <option>Геомембрана</option>
      <option>Бентомат</option>
      <option>Биомат</option>
      <option>Георешетка</option>
      <option>Геотекстиль</option>
      <option>Дренажный мат</option>
      <option>Теплонит</option>
      <option>Анкеры</option>
    </select>
  </div>
 
  <div class="clear"></div>
  <h4>Прикрепите документы по текущему проекту (если имеются)</h4>
  <div class="col_one_third col_last">
 
    <input id="input-3" name="input2[]" type="file" class="file" multiple data-show-upload="false" data-show-caption="true" data-show-preview="true">
  </div>
 
  <div class="col_full hidden">
    <input type="text" id="template-contactform-botcheck" name="template-contactform-botcheck" value="" class="sm-form-control" />
  </div>
 
  <div class="col_full">
    <button class="button button-yellow button-3d nomargin" type="submit" id="template-contactform-submit" name="template-contactform-submit" value="submit">Отправить</button>
  </div>
 
</form>

My sendemail.php code:
<?php
 
require_once('phpmailer/PHPMailerAutoload.php');
 
$toemails = array();
 
$toemails[] = array(
        'email' => '[email protected]', // Your Email Address
        'name' => 'Your Name' // Your Name
      );
 
// Form Processing Messages
$message_success = 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.';
 
// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = 'your-recaptcha-secret-key'; // Your reCaptcha Secret
 
$mail = new PHPMailer();
 
// If you intend you use SMTP, add your SMTP Code after this Line
 
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  if( $_POST['template-contactform-email'] != '' ) {
 
  
    $name = isset( $_POST['template-contactform-name'] ) ? $_POST['template-contactform-name'] : '';
    $email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';
    $phone = isset( $_POST['template-contactform-phone'] ) ? $_POST['template-contactform-phone'] : '';
    $service = isset( $_POST['template-contactform-service'] ) ? $_POST['template-contactform-service'] : '';
 
    $botcheck = $_POST['template-contactform-botcheck'];
 
    if( $botcheck == '' ) {
 
      $mail->SetFrom( $email , $name );
      $mail->AddReplyTo( $email , $name );
      foreach( $toemails as $toemail ) {
        $mail->AddAddress( $toemail['email'] , $toemail['name'] );
      }
      $mail->Subject = "Заявка с сайта geo174.ru";
 
      $name = isset($name) ? "Имя: $name<br><br>" : '';
      $email = isset($email) ? "E-mail: $email<br><br>" : '';
      $phone = isset($phone) ? "Телефон: $phone<br><br>" : '';
      $service = isset($service) ? "Материалы: $service<br><br>" : '';
 
      $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
 
      $body = "$name $email $phone $service $message $referrer";
 
      // Runs only when File Field is present in the Contact Form
      if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {
        $mail->IsHTML(true);
        $mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
      }
 
      // Runs only when reCaptcha is present in the Contact Form
      if( isset( $_POST['g-recaptcha-response'] ) ) {
        $recaptcha_response = $_POST['g-recaptcha-response'];
        $response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );
 
        $g_response = json_decode( $response );
 
        if ( $g_response->success !== true ) {
          echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
          die;
        }
      }
 
      $mail->MsgHTML( $body );
      $sendEmail = $mail->Send();
 
      if( $sendEmail == true ):
        echo '{ "alert": "success", "message": "' . $message_success . '" }';
      else:
        echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }';
      endif;
    } else {
      echo '{ "alert": "error", "message": "Bot <strong>Detected</strong>.! Clean yourself Botster.!" }';
    }
  } else {
    echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }';
  }
} else {
  echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }';
}
 
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rina Furo, 2016-05-06
@ReenaFuro

Hello. I am a beginner programmer, but I myself use something similar on the site
web.furofamily.com/Realia-Real-Eastate
this is a store of ready-made sites, there is no basket.
1. the client marks and fills in the required fields, otherwise - a red mess alert
2. the client clicks on send the order (green mess) - everything comes to me on the soap specified in my cmd_sendmail.php file:
<?
global $EMAIL,$TEL,$COLOR,$FULLSKREEN,$ISTZ,$MEDIAFAQ,$HOSTING,$DOMEN,$MEDIADM,$EDUCATION,$PGID;
global $m_conf;
$PGID = (int)$PGID;
$rm='';
include_once("cls/tsendmail.php");
$mail = new TSendmail();
#if(!$mail->ValidEmail($email)){$rm .= "Please enter a valid e-mail";}
if (!$EMAIL) $rm .= "
if (!$TEL) $rm .= "specify phone
";
if($rm) $this->red_mess .="Please".$rm;
if($FULLSKREEN) $FULLSKREEN="Yes"; else $FULLSKREEN="No";
if($ISTZ) $ISTZ="Yes"; else $ISTZ="No";
if($MEDIAFAQ) $MEDIAFAQ="Yes"; else $MEDIAFAQ="No";
if($HOSTING) $HOSTING="Yes"; else $HOSTING="No";
if($DOMEN) $DOMEN="Yes"; else $DOMEN="No";
if($MEDIADM) $MEDIADM="Yes"; else $MEDIADM="No";
if($EDUCATION) $EDUCATION="Yes"; else $EDUCATION="No";
if (!$rm)
{
$mail->From($m_conf['emailfrom']);
$mail->ClearAddresses();
$mail->To('[email protected]');
$mail->Subject("New order on Yoursite.com ");
!!!! This is part of my CMS. here I get Page Name and Product Name
$pagenaz = GetFieldFromSQL($this->conn,"SELECT NAZ FROM MTREE WHERE ID=".(int)$PGID,'');
!!!!

$soderv = "
Order: $pagenaz
e-mail: $EMAIL
Phone: $TEL
Color scheme: $COLOR
Full screen: $FULLSKREEN
Terms of reference: $ISTZ Website video tutorial
: $MEDIAFAQ
Hosting: $HOSTING
Domain: $DOMEN Video tutorial
for hosting: $MEDIADM Staff
training: $EDUCATION
";
$mail->Body($soderv);
$mail->Send();
$this->green_mess ="Message sent";
}
?>
As you can see, there is a connection of some tsendmail.php sendmail, here is what was at the beginning of the file:
<?php
/*
this class encapsulates the PHP mail() function.
implements CC, Bcc, Priority headers
@version 1.3
- added ReplyTo( $address ) method
- added Receipt() method - to add a mail receipt
- added optionnal charset parameter to Body() method. this should fix charset problem on some mail clients
@example
$m= new Mail; // create the mail
$m->From( "[email protected]" );
$m->To( "[email protected]" );
$m->Subject( "the subject of the mail" );
$message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks.";
$m->Body( $message); // set the body
$m->Cc( "[email protected]");
$m->Bcc( "[email protected]");
$m->Priority(4) ; // set the priority to Low
$m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif
//alternatively u can get the attachment uploaded from a form
//and retreive the filename and filetype and pass it to attach methos
$m->Send(); // send the mail
echo "the mail below has been sent:
", $m->Get(), "";
Saravanan([email protected],[email protected])
*/
I won't give the whole code, only upon request))
3. It is clear that the variables at the beginning of the file correspond to the names of the fields in the form, all inputs, checks, radio , and selects and a button.
There is an incomprehensible part with PGID and some SQL garbage:
$pagenaz = GetFieldFromSQL($this->conn,"SELECT NAZ FROM MTREE WHERE ID=".(int)$PGID,'');
and naz and PGID are internal commands of the CMS Webolla on which the site works, instead of these variables you will have your own, which you take where you take to designate the page from which the order was made and the actual name of the product. This is for a store without a shopping cart.
4. And the last thing, IMHO, your code lacks - layout, in which the script will format the response collected from the form. In what form should you receive the collected mail if there is no layout? Or am I not understanding something? If so, please excuse me, I'm a beginner)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question