Page 1 of 1

Please help with code for SMTP authenticated PHP email form

Posted: Sun Oct 25, 2009 11:45 pm
by web19976
Hello, I'm creating a PHP email form, and for this particular server, I have to use SMTP Authentication. They hosting service sent me this link: http://email.about.com/od/emailprogramm ... cation.htm

Anyway, I have the form created, and I created the PHP file called mail.php. I finally got it to where it does send me an email, but the message is completely blank. My form has four fields, which are: name, email, telephone, and message, so these should each be emailed to me when the form is filled out.

Thank you in advance if anyone can look this over.

Here is my PHP code:

Code: Select all

 
<?php
    require_once "Mail.php";
 
    $from = "Jane Doe <jane.doe@gmail.com>";
    $to = "JD <jim.doe@gmail.com>";
    $subject = "Request to contact me";
   $name = "name:\n";
   $email = "email:\n";
   $phone = "phone:\n";
    $message = "message:\n";
 
    $host = "smtp.gmail.com";
    $username = "jane.doe";
    $password = "blackcat35";
 
    $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 {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=received.htm\">";
     }
    ?>
 
 
And here is my HTML form:

Code: Select all

  
<form method="POST" action="mail.php"> 
<p>Fields marked (*) are required</p>
<p><strong>Name:* </strong><br>
  <input name="name" type="text" size="40" maxlength="50" id="name" /> 
<p><strong>Email:* </strong><br>
<input name="email" type="text" size="40" id="email">
<p><strong>Telephone:</strong><br>
  <input name="message" type="text" size="40" maxlength="30" id="message" /> 
<p><strong>Message:* </strong><br>
<textarea name="message" cols="40" id="message"></textarea>
<p><input type="submit" name="submit" value="Submit">
  <input type="reset" name="Clear" id="Clear" value="Reset" /> 
</form>
 

Re: Please help with code for SMTP authenticated PHP email form

Posted: Mon Oct 26, 2009 2:12 am
by web19976
I'm going to answer my own question in case it comes in handy for somebody. This code works:

Code: Select all

  <?php
    require_once "Mail.php";
 
    $from = "Automated Email>";
    $to = "janedoe@gmail.com";
    $subject = "contact request";
    $name = $_REQUEST['name'] ;
    $email = $_REQUEST['email'] ;
    $telephone = $_REQUEST['telephone'] ;
    $message = $_REQUEST['message'] ;
$body .= "Name: ";
$body .= $_REQUEST['name'];
$body .= "\n";
$body .= "Email: ";
$body .= $_REQUEST['email'];
$body .= "\n";
$body .= "Telephone: ";
$body .= $_REQUEST['telephone'];
$body .= "\n";
$body .= "Message: ";
$body .= $_REQUEST['telephone'];
$body .= "\n";
 
 
    $host = "smtp.gmail.com";
    $username = "iloveblackcats8483930203948";
    $password = "blackcat12559988753";
    
    
 
    $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 {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=itworks.htm\">";
     }
    ?>