【问题标题】:How do I enable SMTP for my PHP contact form?如何为我的 PHP 联系表单启用 SMTP?
【发布时间】:2014-08-23 17:12:46
【问题描述】:

我已经成功地在我的网站上为我的联系表单设置了一个 php 脚本,但我最近发现我的服务器提供商不接受 php。相反,我使用了 SMTP。

任何人都可以帮助我,因为这对我来说是新的。我尝试过使用其他脚本,但无法实现。

这是我的 php 代码:

<?php
/* Set e-mail recipient */
$myemail = "mail@louisreed.co.uk";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Someone has sent you a message";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

我的 HTML 表单:

<div class="row">
            <div class="col-lg-8 col-lg-offset-2">
                <form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
                    <div class="row">
                        <div class="form-group col-xs-12 floating-label-form-group">
                            <label for="name">Name</label>
                            <input class="form-control" type="text" id="name" name="name" placeholder="Name">
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-xs-12 floating-label-form-group">
                            <label for="email">Email Address</label>
                            <input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-xs-12 floating-label-form-group">
                            <label for="subject">Subject</label>
                            <textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-xs-12 floating-label-form-group">
                            <label for="message">Message</label>
                            <textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="form-group col-xs-12">
                            <button type="submit" class="btn btn-lg btn-primary">Send</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

欢迎提出任何建议!

谢谢! :)

路易

【问题讨论】:

  • smtp 与您的代码无关,它位于您本地主机某处的 php.ini 文件中。如果您在使用 SMTP 的 php 中使用 mail(),那么如果您的提供商不支持 php,您就会遇到问题
  • 感谢您的回复,对不起,我听起来像个菜鸟,但我的 php.ini 文件中有什么? - 我已经进行了搜索,但我什至找不到这个文件。干杯
  • 不是找不到。问题是您的主机控制着这个文件。
  • 是的,我的提供商不支持 php,我还有什么其他选择?
  • @SuperDJ 这是我想要避免的!我想要一个漂亮的联系表格而不是使用 mailto:

标签: php forms email smtp contact


【解决方案1】:

好吧,我检查了一些想法。我想您至少可以尝试以下方法。我知道您的主机不支持难以相信的 php。所以也许还有其他问题。你用的是什么主机?

<?php
if($_POST) {
    //Strip user input
    function sanitize($value) {
        //Escape the user input and then strip all html tags
        //mysql_* is not recommended but I guess you don't store data in a database
        $value = mysql_real_escape_string(strip_tags($value));
        //Return the sanitized user input
        return $value;
    }

    //To display all errors
    function errors($error) {
        echo '<ul class="error">';
        foreach($error as $fail) {
            echo '<li>'.$fail.'</li>';
        }
        echo '</ul>';
    }

    //All the input fields
    $name = sanitize($_POST['name']);
    $email = sanitize($_POST['email']);
    $subject = sanitize($_POST['subject']);
    $message = sanitize($_POST['message']);

    //Check if there are no empty fields
    if(!empty($name), !empty($email) && !empty($subject) && !empty($message)) {
        if(strlen($name) <= 3) {
            $error[] = 'Name '.$name.' is too short';
        }

        if(strlen($email) <= 3) {
            $error[] = 'Email '.$email.' is too short';
        }

        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error[] = 'Email '.$email.' is not a valid email';
        }

        if(strlen($subject) <= 3) {
            $error[] = 'Subject '.$subject.' is too short';
        }

        if(strlen($message) <= 8) {
            $error[] = 'Message is too short';
        }

        //If there are no errors
        if(empty($error)) {
            $to = "mail@louisreed.co.uk"; //The email you want to send it to
            //Subject already set
            $headers = "FROM: My site"; //Sets the headers
            //The message for the email
            $message = "Someone has sent you a message using your contact form:\r\n\r\nName: ".$name."\r\nEmail: ".$email."\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
            $mail = mail($to, $subject, $message, $headers);

            //If the mail has been send
            if($mail) {
                header('Location: http://louisreed.co.uk/index.php?success');
                exit();
            }
        }
    } else {
        $error[] = 'There are empty fields';
    }

    //If there are errors show them
    if(!empty($error)) {
        echo errors($error);
    }
}

//If issset success (from the header()) display a message
if(isset($_GET['success'])) {
    echo '<p>Thank you for your message</p>';
}
?>
<form role="form" action="" method="post">
    <div class="row">
        <div class="form-group col-xs-12 floating-label-form-group">
            <label for="name">Name</label>
            <input class="form-control" type="text" id="name" name="name" placeholder="Name">
        </div>
    </div>
    <div class="row">
        <div class="form-group col-xs-12 floating-label-form-group">
            <label for="email">Email Address</label>
            <input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
        </div>
    </div>
    <div class="row">
        <div class="form-group col-xs-12 floating-label-form-group">
            <label for="subject">Subject</label>
            <textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
        </div>
    </div>
    <div class="row">
        <div class="form-group col-xs-12 floating-label-form-group">
            <label for="message">Message</label>
            <textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
        </div>
    </div>
    <br>
    <div class="row">
        <div class="form-group col-xs-12">
            <button type="submit" class="btn btn-lg btn-primary">Send</button>
        </div>
    </div>
</form>

所以试试我上面给你的确切代码,只需将它复制并粘贴到你现在拥有的表单代码上。您可以保留其余代码不变。之后将完整的文件保存为index.php。之后从您的 FTP 中删除 index.html 文件。如果你已经这样做了,你可以上传index.php 文件现在检查你得到了什么。如果还有错误请告诉我

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 1970-01-01
    • 2021-04-16
    • 2017-02-03
    • 2015-04-21
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多