(Noob) - 简单的PHP电子邮件表单代码?

时间:2016-04-15 06:23:37

标签: php

我买了一个引导主题只是为了制作一个简单的网站。它上面有一个联系表格,主题创建者说要确保你的托管让你使用php。我问我的主人他们说:

  

"您需要在表单中添加电子邮件身份验证。

     

在cpanel中创建一个电子邮件地址,以便与表单一起使用,然后使用它   用户名和密码,用于验证从中发送的电子邮件   形式。

     

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm"

我在cpanel中创建了电子邮件,但不幸的是,我对下一步的知识不够了解。我希望有人可以帮助我使用代码来使这个表格正常工作。

这是其中的email.php代码:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$subject = "Message";
$body = "From $name, $email,  \n\n$message";

$to = "myemail@gmail.com";

mail($to, $subject, $body);
?>

我可以添加一些简单的内容来进行身份验证并使表单有效吗?

1 个答案:

答案 0 :(得分:0)

更新回答:

您可以将email.php / html代码编辑为以下内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send email example</title>
</head>
<body>
    <form method="post">
        <input type="text" name="sender_email" placeholder="Senders email address">
        <input type="text" name="receiver_email" placeholder="Receivers email address">
        <input type="text" name="subject" placeholder="Subject">
        <textarea name="message" placeholder="Message"></textarea>
        <button type="submit">Send email</button>
    </form>
</body>
</html>

<?php
    require_once('send_email_class.php');
    if(!empty($_POST)){
        $sender_email = $_POST['sender_email'];
        $receiver_email = $_POST['receiver_email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        $send_email = new send_email_class($sender_email, $receiver_email, $subject, $message);
        $send_email_response = $send_email->send_email();
        echo $send_email_response;
    }
?>

并将以下代码作为名为send_email_class.php的文件上传到您的网络服务器。

<?php
    class send_email_class {
        // Class constructor storing variables
        function __construct($sender, $receiver, $subject, $message){
            $this->receiver = $receiver;
            $this->subject = $subject;
            $this->message = $message;
            $this->header = 'From: ' . $sender . '\r\n';
        }

        // Function to send the email
        function send_email(){
            if(mail($this->receiver, $this->subject, $this->message, $this->headers)){
              return 'Mail sent.';
            }else{
              return 'Unable to send mail.';
            }
        }
    }
?>

当然,您不必从form输入所有这些信息,您可以存储一些信息,例如PHP代码中修复的$sender_email变量。

但这取决于你。希望这有帮助。

修改

请使用以下内容替换email.php的内容:

<?php
    class email{
        // Class constructor storing variables
        function __construct($sender_email, $sender_name, $message){
            $this->sender = $sender_email;
            $this->sender_name = $sender_name;
            $this->message = $message;
            $this->receiver = 'changeme@email.com';
            $this->subject = 'Email from: ' . $sender_name;
            $this->header = 'From: ' . $sender_email . '\r\n';
        }

        // Function to send the email
        function send_email(){
            if(mail($this->receiver, $this->subject, $this->message, $this->header)){
              return 'Thank you ' . $this->sender_name . ' for sending the email containing the following message: ' . $this->message;
            }else{
              return 'Sorry ' . $this->sender_name . ' a error occured, please try again.';
            }
        }
    }

    if(!empty($_POST)){
        $sender_email = $_POST['email'];
        $sender_name = $_POST['name'];
        $message = $_POST['message'];

        $send_email = new email($sender_email, $sender_name, $message);
        $send_email_response = $send_email->send_email();
        echo $send_email_response;
    }
?>

编辑以下代码行:$this->receiver = 'changeme@email.com';

最终答案:

收到文件后,修复很容易,ajax正在处理处理本身。

mail.php内容:

<?php
    $receiver = 'xxxxx@gmail.com';
    $subject = 'Email from: ' . $_POST['name'];
    $message = $_POST['message'];
    $header = 'From: ' . $_POST['email'];

    mail($receiver, $subject, $message, $header);
?>