[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1567
  • Last Modified:

Send SMTP email via PHP form mail

Can someone help me connect these two?  I'm fiddling with the PHP and it's not working.  The idea is to gather all of the information in the form (name, email, wedding date, message, etc) and email it to my address, neatly formatted, and secure.

I don't know how to modify the PHP so it gets all of the fields from the form.

--- form code ----
 
 <form action="sendmail.php" onsubmit="return validate_form(this)" method="POST">
    
    
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
  
  <tr>
    <td width="137" valign="top"><p align="right" class="formtext">  <strong>Name</strong><span class="aster">*</span></p></td>
    <td width="298"><input name="realname" type="text" class="formfield" size="25" /></td>
    </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Email</strong><span class="aster">*</span></p></td>
    <td><input name="email" type="text" class="formfield" size="25" />    </td>
  </tr>
  
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Phone</strong></p></td>
    <td><input name="phone" type="text" class="formfield" size="25" id="phone" /></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Wedding Date</strong><span class="aster">*</span></p></td>
    <td>
 
   <div class="demo">
 
<input name="weddingdate" type="text" class="formfield" id="datepicker" size="9"> 
&nbsp;<span class="alternateinfo"><input name="weddingOn" type="weddingdate-human" class="formfield" id="alternate" size="25"/></span>
</p>
</div><!-- End demo -->   </td>
  </tr>
  
  
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>For the Bride</strong></p></td>
    <td><label>
      <select name="BRIDEservices" id="bride">
        <option value="" selected="selected">Please select...</option>
        <option value="Hair + Makeup">Hair + Makeup</option>
        <option value="Hair only">Hair only</option>
        <option value="Makeup Only">Makeup only</option>
      </select>
    </label></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Bridal Attendants</strong></p></td>
    <td><select name="number-bridal-party" id="bridal-party">
      <option value="NOT-ENTERED" selected="selected">Number</option>
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12+">12+</option>
        </select> 
      for
      <select name="bridal-party-services" id="brideservices">
        <option value="NOT ENTERED" selected="selected">Please select...</option>
        <option value="Hair + Makeup">Hair + Makeup</option>
        <option value="Hair only">Hair only</option>
        <option value="Makeup Only">Makeup only</option>
        <option value="Other">Other (explain below)</option>
      </select></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Details</strong></p></td>
    <td><textarea name="Details" cols="33" rows="12" class="formfield" id="message"></textarea></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong> Preferred Method of Contact</strong></p></td>
    <td><select name="Preferred-Method-of-Contact" id="Preferred-Method-of-Contact">
      <option value="" selected="selected">Please select...</option>
      <option value="Phone">Phone</option>
      <option value="Email">Email</option>
    </select></td>
  </tr>
  <tr>
    <td valign="top">&nbsp;</td>
    <td><input type="submit" src="../../../img/button/submit-form.jpg" name="submit2" alt="Send Feedback" value="Submit" class="submit" /></td>
  </tr>
  
  <tr>
    <td valign="top">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td valign="top"><p class="formtext"><span class="aster">*</span>required</p></td>
    <td><p class="fine">Triple Twist never  collects, shares, or sells any information submitted in this form.</td>
  </tr>
  <tr>
    <td> </td>
    <td></td>
    </tr>
</table>
      
      <input type=hidden name="recipient" value="user@domain.com">
      <input type=hidden name="redirect" value="thankyou.php">
      <input type=hidden name="required" value="realname,email,weddingdate">
      <input type=hidden name="error_redirect" value="reform.php">
      <input type=hidden name="missing_fields_redirect" value="reform.php">
      <input type=hidden name="env_report" value="REMOTE_HOST,HTTP_USER_AGENT,REMOTE_ADDR ">
      <input type=hidden name="print_blank_fields" value="1">
 
 
 
 
    </form>
 
--- mail code ---
 
<?php
require_once "Mail.php";
 
$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test email using PHP SMTP";
$body = "This is a test email message";
 
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$password = "yourPassword";
 
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
$mail = $smtp->send($to, $headers, $body);
 
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

Open in new window

0
jazzviolin
Asked:
jazzviolin
  • 3
  • 3
1 Solution
 
Roger BaklundCommented:
To fetch the fields:

$realname = $_POST['realname'];

To make it secure, you need to remove potential html tags from the input:

$realname = strip_tags($realname);

You can do both in the same operation:

$realname = strip_tags($_POST['realname']);

To append the fields to the email body:

$body .= "Name: $realname\n";

btw, this is wrong: <input name="weddingOn" type="weddingdate-human"

The type attribute should be "text".
0
 
jazzviolinAuthor Commented:
You are the ruler of all things Good and Great and Just.
0
 
jazzviolinAuthor Commented:
Hi there, sorry -- do you know how to make it "from" their email address? right now in the PHP I have:

$from = $_POST['email'];

But when it arrives, it always says it is coming from me (my email address).
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Roger BaklundCommented:
It should work. Can you post your current code?
0
 
jazzviolinAuthor Commented:
Here's the HTML and PHP
   <form action="thankyou.php" onsubmit="return validate_form(this)" method="POST">
    
    
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
  
  <tr>
    <td width="137" valign="top"><p align="right" class="formtext">  <strong>Name</strong><span class="aster">*</span></p></td>
    <td width="298"><input name="realname" type="text" class="formfield" size="25" /></td>
    </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Email</strong><span class="aster">*</span></p></td>
    <td><input name="email" type="text" class="formfield" size="25" />    </td>
  </tr>
  
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Phone</strong></p></td>
    <td><input name="phone" type="text" class="formfield" size="25" id="phone" /></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Wedding Date</strong><span class="aster">*</span></p></td>
    <td>
 
   <div class="demo">
 
<input name="weddingdate" type="text" class="formfield" id="datepicker" size="9"> 
&nbsp;<span class="alternateinfo"><input name="weddinghuman" type="text" class="formfield" id="alternate" size="25"/></span>
</p>
</div><!-- End demo -->   </td>
  </tr>
  
  
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>For the Bride</strong></p></td>
    <td><label>
      <select name="brideservices" id="brideservices">
        <option value="" selected="selected">Please select...</option>
        <option value="Hair + Makeup">Hair + Makeup</option>
        <option value="Hair only">Hair only</option>
        <option value="Makeup Only">Makeup only</option>
      </select>
    </label></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Bridal Attendants</strong></p></td>
    <td><select name="numberbridalparty" id="numberbridalparty">
      <option value="NOT-ENTERED" selected="selected">Number</option>
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12+">12+</option>
        </select> 
      for
      <select name="bridalpartyservices" id="bridalpartyservices">
        <option value="NOT ENTERED" selected="selected">Please select...</option>
        <option value="Hair + Makeup">Hair + Makeup</option>
        <option value="Hair only">Hair only</option>
        <option value="Makeup Only">Makeup only</option>
        <option value="Other">Other (explain below)</option>
      </select></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong>Details</strong></p></td>
    <td><textarea name="details" cols="33" rows="12" class="formfield" id="message"></textarea></td>
  </tr>
  <tr>
    <td valign="top"><p align="right" class="formtext"><strong> Preferred Method of Contact</strong></p></td>
    <td><select name="contactmethod" id="contactmethod">
      <option value="" selected="selected">Please select...</option>
      <option value="Phone">Phone</option>
      <option value="Email">Email</option>
    </select></td>
  </tr>
  <tr>
    <td valign="top">&nbsp;</td>
    <td><input type="submit" src="../../../img/button/submit-form.jpg" name="submit2" alt="Send Feedback" value="Submit" class="submit" /></td>
  </tr>
  
  <tr>
    <td valign="top">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td valign="top"><p class="formtext"><span class="aster">*</span>required</p></td>
    <td><p class="fine"></td>
  </tr>
  <tr>
    <td> </td>
    <td></td>
    </tr>
</table>
      
      <input type=hidden name="recipient" value="xxx@xxx.com">
      <input type=hidden name="redirect" value="thankyou.php">
      <input type=hidden name="required" value="realname,email,weddingdate">
      <input type=hidden name="error_redirect" value="reform.php">
      <input type=hidden name="missing_fields_redirect" value="reform.php">
      <input type=hidden name="env_report" value="REMOTE_HOST,HTTP_USER_AGENT,REMOTE_ADDR ">
      <input type=hidden name="print_blank_fields" value="1">
 
    </form>
   
=== thankyou.php ===
 
 
<?php
require_once "Mail.php";
 
$from = $_POST['email'];
$to = "Email <my@email.com> ";
$subject = "Subject Goes Here";
$realname = strip_tags($_POST['realname']);
$phone = strip_tags($_POST['phone']);
$weddingdate = strip_tags($_POST['weddingdate']);
$weddinghuman = strip_tags($_POST['weddinghuman']);
$brideservices = strip_tags($_POST['brideservices']);
$numberbridalparty = strip_tags($_POST['brideservices']);
$bridalpartyservices = strip_tags($_POST['bridalpartyservices']);
$contactmethod = strip_tags($_POST['contactmethod']);
$details = strip_tags($_POST['details']);
$body .= "Name:     $realname\n\nEmail:      $from\n\nPhone:      $phone\n\nWedding:      $weddingdate\n\nWedding Date:      $weddinghuman\n\nBride:      $brideservices\n\nBridal Attendees:      $numberbridalparty\n\nBridal Attendee Services:      $bridalpartyservices\n\nBest way to contact:      $contactmethod\n\nDetails:      $details\n";
 
$host = "xxx";
$username = "xxx";
$password = "xxx";
 
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
$mail = $smtp->send($to, $headers, $body);
 
if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

Open in new window

0
 
Roger BaklundCommented:
Your code looks correct, I don't know why it does not work. Sorry!

Ask a new question about it, maybe some other expert can offer some insight.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now