如何使用PHP将联系表单信息从网站发送到Godaddy电子邮件?

时间:2017-04-30 18:40:29

标签: php email

我已经分析了许多与此情况有关的类似问题,但我仍然无法成功完成此任务。所以我为冗余道歉。

我创建了一个网站表单,我正在尝试将用户的信息发送到我的GoDaddy Webmail帐户(从那里我将副本发送到我的Gmail帐户)。因此,当我对将用户信息发送到我的电子邮件进行无休止的尝试时,它每次都会失败。

我还尝试在我创建的其他文件中运行PHPMail类,它告诉我它很难连接到SMTP服务器。我确保一切都是正确的,但仍然没有连接。请帮忙!

 HTML FORM: 
<h3>Send us a Message</h3>
            <form name="sentMessage" id="contactForm" novalidate action="contact_me.php" method="post">
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Full Name:</label>
                        <input type="text" class="form-control" id="name" required data-validation-required-message="Please enter your name.">
                        <p class="help-block"></p>
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Phone Number:</label>
                        <input type="tel" class="form-control" id="phone" required data-validation-required-message="Please enter your phone number.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Email Address:</label>
                        <input type="email" class="form-control" id="email" required data-validation-required-message="Please enter your email address.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Message:</label>
                        <textarea rows="10" cols="100" class="form-control" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                    </div>
                </div>
                <div id="success"></div>
                <!-- For success/fail messages -->
                <button type="submit" class="btn btn-primary">Send Message</button>
            </form>
        </div>

 The contact.php form it calls:
      <?php
   // Check for empty fields
   if(empty($_POST['name'])      ||
     empty($_POST['email'])     ||
     empty($_POST['phone'])     ||
     empty($_POST['message'])   ||
     !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
     {
     echo "No arguments Provided!";
     return false;
     }

  $name = strip_tags(htmlspecialchars($_POST['name']));
  $email_address = strip_tags(htmlspecialchars($_POST['email']));
  $phone = strip_tags(htmlspecialchars($_POST['phone']));
  $message = strip_tags(htmlspecialchars($_POST['message']));

  // Create the email and send the message
  $to = 'cochranlawoffice@cochranlawoffice.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
  $email_subject = "Website Contact Form:  $name";
  $body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
  $headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
  $headers .= "Reply-To: $email_address";   

  /*$smtp = Mail::factory('smtp', array(
          'host' => 'localhost',
          'port' => '25',
          'auth' => true,
          'username' => 'cochranlawoffice@cochranlawoffice.com', //your gmail account
          'password' => 'password' // your password
      )); 

  // Send the mail
  $mail = $smtp->send($to, $email_subject, $headers, $body); */

  mail($to,$email_subject,$body,$headers);
  return true;         
  ?>

以下是我用来运行PHPMail并收到无法连接错误的类:

  <?php
    /*
     *
     * This example shows how to handle a simple contact form.
     */
    $msg = '';
    //Don't run this unless we're handling a form submission
    if (array_key_exists('email', $_POST)) {
        date_default_timezone_set('Etc/UTC');
        require './PHPMailer/PHPMailerAutoload.php';
        //Create a new PHPMailer instance

        $mail = new PHPMailer(); 

        $mail->IsSMTP(); 
        $mail->SMTPDebug = 1; 
        $mail->SMTPAuth = true; 
        $mail->SMTPSecure = 'tls'; 
        $mail->Host = "localhost";

        $mail->Port = 25; 
        $mail->IsHTML(true);

        $mail->Username = "cochranlawoffice@cochranlawoffice.com";
        $mail->Password = "password";
        //Use a fixed address in your own domain as the from address
        //**DO NOT** use the submitter's address here as it will be forgery
        //and will cause your messages to fail SPF checks
        $mail->setFrom('donotreply@email.com', 'name');
        //Send the message to yourself, or whoever should receive contact for submissions
        $mail->addAddress('cochranlawoffice@cochranlawoffice.com', 'John Doe');
        //Put the submitter's address in a reply-to header
        //This will fail if the address provided is invalid,
        //in which case we should ignore the whole request
        if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
            $mail->Subject = 'PHPMailer contact form';
            //Keep it simple - don't use HTML
            $mail->isHTML(false);
            //Build a simple message body
            $mail->Body = <<<EOT
    Email: {$_POST['email']}
    Name: {$_POST['name']}
    Message: {$_POST['message']}
    EOT;
            //Send the message, check for errors
            if (!$mail->send()) {
                //The reason for failing to send will be in $mail->ErrorInfo
                //but you shouldn't display errors to users - process the error, log it on your server.
                $msg = 'Sorry, something went wrong. Please try again later. Error: ' . $mail->ErrorInfo;

            } else {
                $msg = 'Message sent! Thanks for contacting us.';
            }
        } else {
            $msg = 'Invalid email address, message ignored.';
        }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Contact form</title>
    </head>
    <body>
    <h1>Contact us</h1>
    <?php if (!empty($msg)) {
        echo "<h2>$msg</h2>";
    } ?>
    <form method="POST">
        <label for="name">Name: <input type="text" name="name" id="name"></label><br>
        <label for="email">Email address: <input type="email" name="email" id="email"></label><br>
        <label for="message">Message: <textarea name="message" id="message" rows="8" cols="20"></textarea></label><br>
        <input type="submit" value="Send">
    </form>
    </body>

0 个答案:

没有答案