issue sending email using form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peach
Forum Newbie
Posts: 5
Joined: Tue Nov 20, 2007 8:17 am

issue sending email using form

Post by peach »

Hi everybody:

I am new to php. I am trying to get this below code to work. I got this from some other forum. Now the issue is I don't get any email after i hit send. It is not showing any error message or anything. I think the form is working, but i am not getting the email. I try to test this using my personal email ID. Can anyone help me to fix the issue?
thanks in advance
Peach

Code: Select all

<?php
      # home page for link at top of page:
 
      $home = "http://www.xxxx.com/xx.html";
 
      # Modify the following variables to set up where emails will be sent.
 
      # $toName will be concatenated with a "@" and then $toDomain to create
 
      # the complete email address.
 
      $toName   = "microsoft_sucks0";         # the part that goes before the @
  
      $toDomain = "yahoo.com";  # the part that comes after the @
 
      # To cc someone:

      $ccName   = "ccname";
  
      $ccDomain = "ccdomain.com";
  
      #  To bcc someone:
  
      $bccName   = "bccname";
  
      $bccDomain = "bccdomain.com";
 
      #################################
 
      # end of user-defined variables
  
      ##################################
  
      # email address validation function
 
      function is_valid_email_address($email) { 
  
        $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'; 
 
        $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'; 
 
        $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.

                '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'; 
 
        $quoted_pair = '\\x5c\\x00-\\x7f'; 
  
        $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d"; 

        $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22"; 

        $domain_ref = $atom; 

        $sub_domain = "($domain_ref|$domain_literal)"; 
 $word = "($atom|$quoted_string)"; 
 
        $domain = "$sub_domain(\\x2e$sub_domain)*"; 
 
        $local_part = "$word(\\x2e$word)*"; 
 
        $addr_spec = "$local_part\\x40$domain"; 
 
        return preg_match("!^$addr_spec$!", $email) ? 1 : 0;

      }
 
      # Init. some variables:
 
      $error = "";
 
      $success = FALSE;

      # process form if submitted:
  
      if(isset($_POST['submit'])){
 
        foreach($_POST as $key => $val)  {
  
            if (empty($_POST[$key]))    {
  
               $error .= "You must enter a value for " .
  
                          ucwords(str_replace("_"," ",$key)) . ". ";
 
            }   
 
            else  {
 
               if(get_magic_quotes_gpc())      {
 
                    $_POST[$key] = stripslashes($val);     
 
               }   
  
            }   
         if($key != 'message')    {
  
               $_POST[$key] = preg_replace('/[\r\n]/', ' ', $val);
  
            } 
  
        } 
 
        if($_POST['email_address'] != $_POST['repeat_email'])  {
  
          $error .= "Email Address is not the same as Repeat Email. "; 
  
        } 

        elseif(is_valid_email_address($_POST['email_address']) == 0)  {
 
          $error .= "'{$_POST['email_address']}' does not appear to be a valid email address. ";
  
        } 
  
        if(empty($error))  # no errors in input, so go ahead and email it. 
 
        {
  
          $to = "$toName@$toDomain";
 
          $headers = "From: ".preg_replace('/[\r\n]+/',' ', $_POST['email_address']);
 
          if(!empty($ccName) and !empty($ccDomain))    {
  
            $headers .= "\r\nCc: $ccName@$ccDomain";
  
          }

          if(!empty($bccName) and !empty($bccDomain))    {
 
            $headers .= "\r\nBcc: $bccName@$bccDomain\r\n\r\n";   
  
          }   
  
          $headers .= "\r\nX-Mailer: PHP/" . phpversion();
 
          $msg = "From {$_POST['name']} ({$_POST['email_address']})";
  
          $msg .= "\n\n\n{$_POST['message']}";
 
          echo "<pre>$headers"; exit;
  
          $result = @mail($to,
                      stripslashes($_POST['subject']),

                          $msg,
  
                          $headers);
 
          if(!$result)    {
  
            $error = "There was an unknown error while attempting to send your email.";
  
          }
    else    {
  
            header("Location: http://www.charles-reace.com/Email_Me/thanks.php");
 
          }
  
        }
  
      }
      ?>
	  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

      <html lang='en'>
 
      <head>
 
      <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>

      <title>Email Me</title>
  <style type="text/css">
 
      body { width: 950; background: #E7EFD6 ; color:#002200; margin: 0px 150px 0 37px; 
	  font-family:verdana,arial,sans serif; font-size:12px;}
 
      .lab {margin-left:3px;display: block; float: left; width: 9em;}

      legend {font-size:13px;font-weight:bold;}
 .img-header {clear: both; float: left; width: 950px; height: 90px; margin: 0px; padding: 0px;} 
      </style>
 
      </head>

      <body>

      <?php
 
      if(!empty($error)){
 
        echo "<p style='color: #c03;'>$error</p>\n";
 
      }
 
      ?>

      <h2>Email Me</h2>
 
      <form action='<?php echo $_SERVER['PHP_SELF'] ?>' method=post>

      <fieldset>

      <legend>Your Contact Information</legend>
 
      <p><label for='name' class='lab'>Name:</label>

      <input type='text' name='name' id='name' size='30' maxlength='40'<?php
 
      if(!empty($_POST['name'])){ 
 
        echo "value='{$_POST['name']}'";
 
      }

      ?>></p>
 
      <p><label for='email_address' class='lab'>Email Address:</label>
 <input type='text' name='email_address' id='email_address' size='30' maxlength='40'<?php
 
      if(!empty($_POST['email_address'])){ 
 
        echo "value='{$_POST['email_address']}'";

      }
 
      ?>></p>
 
      <p><label for='repeat_email' class='lab'>Repeat Email:</label>
 
      <input type='text' name='repeat_email' id='repeat_email' size='30' maxlength='40'<?php
 
      if(!empty($_POST['repeat_email'])) { 
 
        echo "value='{$_POST['repeat_email']}'";
 
      }
 
      ?>></p>
 
      </fieldset>
 
      <p> </p>
 
      <fieldset>
 
      <legend>Message</legend>

      <p><label for='subject' style="margin-left:3px;display: block; float: left; width: 9em;">Subject:</label>
 
      <input type='text' name='subject' id='subject' size='50' maxlength='60'<?php
 
      if(!empty($_POST['subject'])){ 

        echo " value='{$_POST['subject']}'";

      }
 
      ?>></p>

      <p><label for='message' style="margin-left:3px;display: block; float: left; width: 9em;">Message:</label>
 
      <textarea name='message' id='message' cols='50' rows='8'
 
      style="width: 375px"><?php

      if(!empty($_POST['message'])){ 
 
        echo $_POST['message'];
 
      }
 
      ?></textarea></p>

      <p style="text-align: center;"><input type='submit' name='submit' value="Send Email"></p>
 
      </fieldset>
 
      </form>
	  
 
      </body>
 
      </html>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Maybe it is this line right before the mail() function is called that is causing the problem:

Code: Select all

echo "<pre>$headers"; exit;
(#10850)
peach
Forum Newbie
Posts: 5
Joined: Tue Nov 20, 2007 8:17 am

Post by peach »

arborint:

Thanks for the reply. Like i said before i am new to php, can i know whats wrong with that line?
thanks once again for the reply
Peach
arborint wrote:Maybe it is this line right before the mail() function is called that is causing the problem:

Code: Select all

echo "<pre>$headers"; exit;
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

exit; will stop the script execution at that point, so it is never getting to the line with email(). Comment out that line and then try it:

Code: Select all

//               echo "<pre>$headers"; exit;
(#10850)
peach
Forum Newbie
Posts: 5
Joined: Tue Nov 20, 2007 8:17 am

Post by peach »

Thanks once again for the help. I did try your suggestion but i got an error message saying "There was an unknown error while attempting to send your email."
I don't know if i was clear in my first post. This is whats happening with the present code (without your suggestion)
I get a page saying "From: microsoft_sucks0@yahoo.com
Cc: ccname@ccdomain.com
Bcc: bccname@bccdomain.com
X-Mailer: PHP/4.3.11"""

But i never get an email in my mail box
Once again thanks
arborint wrote:exit; will stop the script execution at that point, so it is never getting to the line with email(). Comment out that line and then try it:

Code: Select all

//               echo "<pre>$headers"; exit;
Last edited by peach on Tue Nov 20, 2007 1:37 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might want to remove the error suppression character before the email() function call by changing "@mail(" to "mail(" so your script will show errors (if error_reporting is set to show errors).
(#10850)
peach
Forum Newbie
Posts: 5
Joined: Tue Nov 20, 2007 8:17 am

Post by peach »

I did try your suggestion. It did not give any error. It gave me the same result. Gave me the same window with this
From: microsoft_sucks0@yahoo.com
Cc: ccname@ccdomain.com
Bcc: bccname@bccdomain.com


X-Mailer: PHP/4.3.11
arborint wrote:You might want to remove the error suppression character before the email() function call by changing "@mail(" to "mail(" so your script will show errors (if error_reporting is set to show errors).
peach
Forum Newbie
Posts: 5
Joined: Tue Nov 20, 2007 8:17 am

Post by peach »

Hi everybody:
as per arborint suggestion i deleted this line (echo "<pre>$headers"; exit;) and the code worked in my local machine using IIS.
I am using yahoo hosting and they use apache webserver. This code do not work in their server. I talked to yahoo guys and they could not help. Can anybody here help me?
Thanks in million in advance for any help
Peach
Post Reply