如何在php中手动更改联系表单的电子邮件

时间:2012-10-29 23:26:10

标签: php wordpress email wordpress-theming

我正在使用Wordpress主题,并且更改电子邮件设置表单的选项无效。因此,默认情况下,联系表单中的电子邮件会发送到管理员电子邮件。

我想更改联系表单的位置(即:联系表格的负责人)。因此,而不是试图解决主题问题。我宁愿手动输入电子邮件EXAMPLE@MAIL.COM以便它可以正常工作。

以下是联系人模板的代码

  <?php
/*
Template Name: Contact
*/
?>

<?php
if(isset($_POST['submitted'])) {
          if(trim($_POST['contactName']) === '') {
               $nameError = 'Please enter your name.';
               $hasError = true;
          } else {
               $name = trim($_POST['contactName']);
          }

          if(trim($_POST['email']) === '')  {
               $emailError = 'Please enter your email address.';
               $hasError = true;
          } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
               $emailError = 'You entered an invalid email address.';
               $hasError = true;
          } else {
               $email = trim($_POST['email']);
          }

          if(trim($_POST['comments']) === '') {
               $commentError = 'Please enter a message.';
               $hasError = true;
          } else {
               if(function_exists('stripslashes')) {
                    $comments = stripslashes(trim($_POST['comments']));
               } else {
                    $comments = trim($_POST['comments']);
               }
          }

          if(!isset($hasError)) {
               $emailTo = get_option('workspace_email');
               if (!isset($emailTo) || ($emailTo == '') ){
               $emailTo = get_option('admin_email');
               }
               $subject = '[Contact Form] From '.$name;
               $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
               $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

               mail($emailTo, $subject, $body, $headers);
               $emailSent = true;
          }

} ?>

<?php get_header(); ?>

        <div id="content">
               <h1 class="page-title"><?php the_title(); ?></h1>       
              <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                   <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
                    <div class="entry-content">
                         <?php the_content(); ?>
                         <?php if(isset($emailSent) && $emailSent == true) { ?>

                          <div class="thanks">
                              <p><?php _e('Thanks, your email was sent successfully. ', 'themejunkie') ?></p>
                          </div><!-- .thanks -->

                      <?php } else { ?>

                          <?php if(isset($hasError) || isset($captchaError)) { ?>
                              <p class="error"><?php _e('Sorry, an error occurred.', 'themejunkie') ?><p>
                          <?php } ?>

                          <form action="<?php the_permalink(); ?>" id="contact-form" method="post" class="clear">
                              <ul class="contact-form">
                                  <li><label for="contactName"><?php _e('Name:', 'themejunkie') ?></label>
                                      <input class="txt" type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
                                      <?php if($nameError != '') { ?>
                                          <span class="error"><?php echo $nameError; ?></span>
                                      <?php } ?>
                                  </li>

                                  <li><label for="email"><?php _e('Email:', 'themejunkie') ?></label>
                                      <input class="txt" type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
                                      <?php if($emailError != '') { ?>
                                          <span class="error"><?php echo $emailError; ?></span>
                                      <?php } ?>
                                  </li>

                                  <li class="textarea"><label for="commentsText"><?php _e('Message:', 'themejunkie') ?></label>
                                      <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
                                      <?php if($commentError != '') { ?>
                                          <p><span class="error"><?php echo $commentError; ?></span></p>
                                      <?php } ?>
                                  </li>

                                  <li>
                                      <input type="hidden" name="submitted" id="submitted" value="true" />
                                      <input name="submit" type="submit" id="submit" class="submit-button" value="<?php _e('Send Email', 'themejunkie') ?>" />
                                  </li>
                              </ul>
                          </form>
                      <?php } ?>
                    </div><!-- .entry-content -->
               </div><!-- #post-<?php the_ID(); ?> -->
               <?php endwhile; endif; ?>
          </div><!-- #content -->

<?php get_sidebar(); ?>            
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:1)

您发送到的电子邮件地址是mail()功能的第一个参数:

mail('EXAMPLE@MAIL.COM', $subject, $body, $headers);

这不是一个好方法,而是一个快速修复。请务必不要更改或删除原始行,但只能将其注释掉并留下有用的评论,说明您做了什么以及为什么:

//mail($emailTo, $subject, $body, $headers);
//Novice change: manually entered an email address, not knowing the correct way.
mail('EXAMPLE@MAIL.COM', $subject, $body, $headers);

不要把它当作个人,但也许你应该要求/雇用某人来实际知道Wordpress和PHP。