Php 发送电子邮件按钮

Php 发送电子邮件按钮,php,jquery,html,css,wordpress,Php,Jquery,Html,Css,Wordpress,我正在开发一个用HTML、CSS、jQuery和PHP进行Wordpress编码的网站,我需要创建一个包含以下信息的按钮:姓名、邮件和消息,当用户单击“发送”并填充这些信息时,它将发送到一封电子邮件,我的问题是如何使按钮工作,我正在一个实时网站上测试它。另外,我在哪里定义电子邮件,它将在哪里接收所有信息 <?php //response generation function $response = ""; //function to generate respo

我正在开发一个用HTML、CSS、jQuery和PHP进行Wordpress编码的网站,我需要创建一个包含以下信息的按钮:姓名、邮件和消息,当用户单击“发送”并填充这些信息时,它将发送到一封电子邮件,我的问题是如何使按钮工作,我正在一个实时网站上测试它。另外,我在哪里定义电子邮件,它将在哪里接收所有信息

    <?php

  //response generation function

  $response = "";

  //function to generate response
  function my_contact_form_generate_response($type, $message){

    global $response;

    if($type == "success") $response = "<div class='success'>{$message}</div>";
    else $response = "<div class='error'>{$message}</div>";

  }

  //response messages
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please supply all information.";
  $email_invalid   = "Email Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";

  //user posted variables
  $name = $_POST['message_name'];
  $email = $_POST['message_email'];
  $message = $_POST['message_text'];
  $human = 2;

  //php mailer variables
  $to = get_option('admin_email');
  $subject = "Someone sent a message from ".get_bloginfo('name');
  $headers = 'From: '. $email . "\r\n" .
    'Reply-To: ' . $email . "\r\n";

  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {

      //validate email
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        my_contact_form_generate_response("error", $email_invalid);
      else //email is valid
      {
        //validate presence of name and message
        if(empty($name) || empty($message)){
          my_contact_form_generate_response("error", $missing_content);
        }
        else //ready to go!
        {
          $sent = wp_mail($to, $subject, strip_tags($message), $headers);
          if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
          else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
        }
      }
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

?>

<?php get_header(); ?>

  <div id="primary" class="site-content">
    <div id="content" role="main">

      <?php while ( have_posts() ) : the_post(); ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <header class="entry-header">
              <h1 class="entry-title"><?php the_title(); ?></h1>
            </header>

            <div class="entry-content">
              <?php the_content(); ?>

              <style type="text/css">
                .error{
                  padding: 5px 9px;
                  border: 1px solid red;
                  color: red;
                  border-radius: 3px;
                }

                .success{
                  padding: 5px 9px;
                  border: 1px solid green;
                  color: green;
                  border-radius: 3px;
                }

                form span{
                  color: red;
                }
              </style>

              <div id="respond">
                <?php echo $response; ?>
                <form action="<?php the_permalink(); ?>" method="post">
                  <p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
                  <p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
                  <p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
                  <input type="hidden" name="submitted" value="1">
                  <p><input type="submit"></p>
                </form>
              </div>


            </div><!-- .entry-content -->

          </article><!-- #post -->

      <?php endwhile; // end of the loop. ?>

    </div><!-- #content -->
  </div><!-- #primary -->

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

@Matthew Goheen所述的
;PHPMailer会有很大帮助。
您需要一个带有按钮的html表单:

<form action="phpmailingscript.php" method="POST">
    <input type="text" name="information"></input>
    <button type="submit">Submit</button>
</form>

提交
此表单将表单数据发布到phpmailingscript.php。 要从php脚本访问此信息,请执行以下操作:

<?php   
if(isset($_POST['information']){
    $information = $_POST['information'];
}

首先,您需要创建一个html表单,该表单可以获取用户输入的数据,并通过POST将其提交到PHP文件。(见艾萨克的答复)

首先测试将数据发布到另一个页面并回显所有变量的功能,以确保您可以在下一个页面上看到它们。例如,在Isaac的代码中,他将数据发布到的文件命名为“phpmailingscript.php”

一旦数据成功发布到“phpmailingscript.php”,请使用 发送邮件

下面是我为我的工作使用而编写的示例代码,它可以完美地工作。可能需要进行一些调整以适应您的需求,但应该为您提供一个良好的起点$mail=new PHPMailer()


PHPMailer可能会帮上不少忙。它会发送到同一个电子邮件地址吗?或者,根据用户的不同,电子邮件地址每次都会不同吗?发送到相同的电子邮件地址
  $message_header = "This is the message header\n;

  $message_footer = "This is the message footer";


  // Email FILE

  $filename = "you can define an attachment name here. delete this if not needed";
  $file_to_attach = "enter/the/filepath/to/the/attachment/here";


  $message_body = "This is the body of the message";
  $message = $message_header.$message_body.$message_footer;

  //you can change this to anything
  $mail->addReplyTo("noreply@website.com");
  $mail->From          = "noreply@website.com";
  $mail->FromName      = "noreply@website.com";
  $mail->Sender        = "noreply@website.com";



  /add the email address here
  // Add a BCC recipient, copy and paste the below line for multiple recepients
  $mail->AddBCC( "johndoe@hotmail.com" );
  $mail->AddBCC( "johndoe2@gmail.com" );

  // Set email format to plain text
  $mail->IsHTML( false );                                

  $mail->Subject = "Enter the subject of the message";
  $mail->Body    = $message;

  //add the attachment from above. 
  //delete if not needed
  $mail->AddAttachment( $file_to_attach, $filename );      

  $mail->send();