## Sending Email

Mail service -- e.g. Mailgun, Sendgrid, etc.

### submit.php Example (uses Mailgun)

    <?php require_once 'vendor/autoload.php';

         $loader = new \Twig\Loader\FilesystemLoader('./templates');
         $twig = new \Twig\Environment($loader, [
             'cache' => './compilation_cache',
             'debug' => true,
             'auto_reload' => true
         ]);
         $requestTime = new DateTime();
         $requestTime->setTimestamp($_SERVER['REQUEST_TIME']);
         $requestTimeHuman = $requestTime->format('Y-m-d\TH:i:s');

         $echocontent = $twig->render('form-response-template.html',
              [
              'getparams' => $_GET,
              'postparams' => $_POST,
              'httpheaders' => getallheaders(),
              'httpmethod' => $_SERVER['REQUEST_METHOD'],
              'serverinfo' => $_SERVER,
              'requesttime' => $requestTimeHuman,
              'httpreferer' => $_SERVER['HTTP_REFERER']
              ]);
         ?>

         <?php

         require 'vendor/autoload.php';
         use Mailgun\Mailgun;

         $mailto = filter_var($_POST['__mailto'], FILTER_VALIDATE_EMAIL);
         $mailkey = '';
         if (preg_match('/^csci(e|s)12\-(spring|summer|fall)-\d{4}-[a-z]+[0-9]+$/',$_POST['__mailkey'])) {
            $mailkey = $_POST['__mailkey'];
         }

         // only send if POST and if __mailto and if __mailkey exist

         if (strcmp($_SERVER['REQUEST_METHOD'],'POST') == 0 && $mailto && $mailkey ) {
           error_log("FormSubmit | MAILGUN | $mailkey | $mailto |");
           // First, instantiate the SDK with your API credentials
           $MG_API_KEY = getenv('MG_API_KEY');
           $MG_DOMAIN = getenv('MG_DOMAIN');
           $MG_ = getenv('MG_');

           $mg = Mailgun::create($MG_API_KEY); // For US servers
           $domain = $MG_DOMAIN;

           // Now, compose and send your message.
           // $mg->messages()->send($domain, $params);
           $mg->messages()->send($domain, [
             ''    => $MG_,
             'to'      => $mailto,
             'subject' => 'Form Submission',
             'html'    => $echocontent
           ]);
         }

         echo($echocontent);
         ?>

### form_response_template.html

This is a PHP "Twig" template.

    <DOCTYPE html>
         <html lang="en">
           <head>
            <title>Form Submission</title>
            <style><!-- inline styles removed for clarity --></style>
           </head>

           <body>
             <h1>Form Submission</h1>
             {% if getparams %}
             <h2>GET Parameters</h2>
             <table>
               <thead>
                 <tr><th>Name</th><th>Value</th></tr>
               </thead>
               <tbody>
               {% for key, value in getparams %}
               <tr>
                 <th>{{ key }}</th>
                 <td>
                   {% if value is iterable %}
                   {% for v in value %}
                     {{ v }}{% if not(loop.last) %},<br/>{% endif %}
                   {% endfor %}
                   {% else %}
                     {{ value }}
                   {% endif %}
                 </td>
               </tr>
               {% endfor %}
               </tbody>
             </table>
             {% endif %}

             {% if postparams %}
             <h2>POST Parameters</h2>
             <table>
               <thead>
                 <tr><th>Name</th><th>Value</th></tr>
               </thead>
               <tbody>
               {% for key, value in postparams %}
               <tr>
                 <th>{{ key }}</th>
                 <td>
                   {% if value is iterable %}
                   {% for v in value %}
                     {{ v }}{% if not(loop.last) %},<br/>{% endif %}
                   {% endfor %}
                   {% else %}
                     {{ value }}
                   {% endif %}
                 </td>
               </tr>
               {% endfor %}
               </tbody>
             </table>
             {% endif %}

             {% if serverinfo %}
             <h2>Request Information</h2>
             <table>
               <thead>
                 <tr><th>Name</th><th>Value</th></tr>
               </thead>
               <tbody>
                 <tr><th>Request Time</th><td>{{ requesttime }}</td></tr>
                 <tr><th>Remote Address</th><td>{{ serverinfo['REMOTE_ADDR'] }}</td></tr>
                 <tr><th>HTTP Referrer</th><td><a href="{{ httpreferer }}">{{ httpreferer }}</a></td></tr>
                 <tr><th>HTTP Method</th><td>{{ httpmethod }}</td></tr>
               </tbody>
             </table>
             {% endif %}

             <footer>
               <p>This form submission tool is only for use by current students in <a href="https://cscie12.dce.harvard.edu/">CSCI E-12</a> and <a href="https://cscis12.dce.harvard.edu/">CSCI S-12</a> courses.</p><p>David Heitmeyer | <a href="https://extension.harvard.edu/">Harvard Extension School</a> | <a href="https://summer.harvard.edu/">Harvard Summer School</a></footer>
           </body>
          </html>