邮件没有被发送

时间:2013-12-17 13:59:44

标签: oop php

我正在VPS上对此网站进行测试。邮件功能目前没有发送任何电子邮件。这可能是代码错误,或者我可能无法在代码中看到某些内容。

如果有人可以看,告诉我我做错了什么。我不确定我的代码是否有些奇怪,或者它是我的VPS。我以前使用我的VPS发送了电子邮件,并且它已经在其他网站上工作,所以我不确定为什么这不起作用。

Mail.php

class Mail {

    protected $to;
    protected $title;
    protected $email;
    protected $fullName;
    protected $company;
    protected $enquiry;
    protected $message;

    public function __construct() {
        $this->email = $_POST['email'];
        $this->fullName = $_POST['fullName'];
        $this->company = $_POST['company'];
        $this->enquiry = $_POST['enquiry'];
        return;
    }

    function setToTitle($to, $title) {
        $this->to = $to;
        $this->title = $title;
    }

    function getToTitle() {
        return $this->to;
        return $this->title;
    }

    function setMessage($message) {
        $this->message = $message;
    }

    function getMessage() {
        return $this->message;
    }

    function setEmail($email) {
        $this->email = $email;
    }

    function setFullName($fullName) {
        $this->fullName = $fullName;
    }

    function setCompany($company) {
        $this->company = $company;
    }

    function setEnquiry($enquiry) {
        $this->enquiry = $enquiry;
    }

    function getEmail() {
        return $this->email;
    }

    function getFullName() {
        return $this->fullName;
    }

    /**
     * Sends the email
     */
    function sendMail() {
        $headers = array();
        $headers[] = "MIME-Version: 1.0";
        $headers[] = "Content-type: text/plain; charset=utf-8";
        $headers[] = "From: testing <test@test.com>";
        $headers[] = "X-Mailer: PHP/".phpversion();
        mail($this->to, $this->title, $this->getMessage(), implode("\r\n", $headers));
    }

    /**
     * Sends a product enquiry email
     * Automatically sets the to address, title and content
     */
    function sendContactEmail() {
        $this->to = $this->getEmail();
        $this->title = 'Semtronics Product Enquiry';
        $s = 'Hello, ' . $this->getFullName();
        $s .= "\nThank you for your product enquiry, we will contact you as soon as possible.";
        $s .= "\nKind Regards,";
        $s .= "\nTest Team";
        $this->message = $s;
        $this->sendMail();
    }

Controller.php这样

class Controller {

    protected $loginHandler;
    protected $view;

    function __construct() {
        $this->loginHandler = new LoginHandler();
    }

    function getLoginHandler() {
        return $this->loginHandler;
    }
}

ContactPageController.php:

class ContactPageController extends Controller {

    function invoke() {
        if(isset($_GET['action'])) {
            $action = $_GET['action'];
            if ($action == 'contactUs') {
                return $this->sendContactEnquiry();
            }
        }
        $view = new ContactPageView($this);
        $view->display();
    }

    function sendContactEnquiry() {

        // Validate full name
        if (!isset($_POST['fullName']) || $_POST['fullName'] == '') {
            $this->view = new ContactPageView($this);
            $this->view->display('Please enter your full name.');
            return;
        }

        // Validate email
        if (!isset($_POST['email']) || $_POST['email'] == '') {
            $this->view = new ContactPageView($this);
            $this->view->display('Please enter your email address.');
            return;
        }

        // Validate enquiry
        if (!isset($_POST['enquiry']) || $_POST['enquiry'] == '') {
            $this->view = new ContactPageView($this);
            $this->view->display('Please fill out your enquiry.');
            return;
        }

        $mail = new Mail();
        $mail->sendContactEmail();
        var_dump($mail);
        $this->view = new ContactPageView($this);
        $this->view->display('Thank you for your enquiry!');
    }
}

ContactPageView.php:

class ContactPageView extends View {

    function display($error = null) {

        include APPROOT . 'include/formHelpers.php';

        $title = 'Contact Us';
        $body = 'contactpage.php';

        include(HTMLROOT . 'page.php');
    }
}

contactpage.php:

<section class="contact-container left s-container">
    <div class="center">
        <h2>Product Enquiry</h2>
        <form method="post" action="<?php echo htmlspecialchars(SITEROOT . '?location=contact&action=contactUs'); ?>">
        <?php if (isset($error)): ?>
            <div class="error">
                <p><?php echo htmlspecialchars($error); ?></p>
            </div>
        <?php endif; ?>
            <label for="fullName">Full Name<input type="text" name="fullName" id="fullName" ></label>
            <label for="email">Email<input type="text" name="email" id="email" placeholder="someone@example.com" ></label>
            <label for="company">Company (Optional)<input type="text" name="company" id="company" ></label>
            <label for="enquiry">Enquiry<textarea name="enquiry" rows="10" cols="50" id="enquiry" placeholder="What would you like to know?" ></textarea></label>
            <input class="btn-blue" type="submit" name="submit" value="Submit Enquiry">
        </form>
    </div>
</section>

1 个答案:

答案 0 :(得分:0)

我建议您将原生mail()替换为其他一些复杂的工具,例如 PHPMailer

一些好处:

  1. 少代码
  2. 易于维护
  3. 更多功能(HTML邮件等)
  4. 它使用您的SMTP / POP3邮件服务器。所以结束垃圾邮件的可能性更小!