开发者

Email class PHP stopped working

I had this working and I don't know what I did that made it stop. Here is my Email class:

<?php

    //  File Details: Base mail class

class Email {

    private $to = 'scott@...';
    public $email = NULL;
    public $phone = NULL;
    public $subject = NULL;
    public $body = NULL;
    //private $magic_quotes_active;
//  private $real_escape_string_exists;

    //function __construct() {
        //$this->magic_quotes_active = get_magic_quotes_gpc();
        //$this->real_escape_string_exists = function_exists("mysql_real_escape_string");
    //}

    public function send() {
        $this->addHeader('From: moreinfo@ulsinc.com' . "\r\n" .
                         'Reply-To: moreinfo@ulsinc.com' . "\r\n" .
                         'X-Mailer: PHP/' . phpversion() . "\r\n");
        $this->addHeader("MIME-Version: 1.0\r\n");
        $this->addHeader("Content-Type: text/html; charset=ISO-8859-1\r\n");
        $success = mail($this->to, $this->subject, $this->body, 开发者_运维知识库$this->headers);
        if ($success) {
            return true;
        } else {
            return false;
        }
    }

    private function addHeader($header) {
        $this->headers .= $header;
    }

    public function setName($first, $last) {   
        $this->first = $first;
        $this->last = $last; 
    }

    public function setSubject($subject) {   
        $this->subject = $subject; 
    }

    public function setPhone($phone) {   
        $this->phone = $phone; 
    }

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


    public function setBody($body) {   
        //$this->body = $this->escape_value($body);
        $this->body = $body;
    }

    //public function escape_value($value) {
//      if ($this->real_escape_string_exists) { // PHP 4.3.0 or higher
//          // undo any magic quote effects so mysql_real_escape_string can do the work
//          if ($this->magic_quotes_active) {
//              $value = stripslashes($value);
//          }
//          $value = mysql_real_escape_string($value);
//          
//      } else { // before PHP v.4.3.0
//          //if magic quotes aren't already on then add slashes manually
//          if (!$magic_quotes_active) {
//              $value = addslashes($value);
//          }
//      }
//      
//      return $test; 
//  }

}

$mail = new Email();    
$mail->setSubject($_POST['subject']);    
$mail->setBody($_POST['body']); 
$mail->setName($_POST['first'], $_POST['last']);
$mail->setEmail($_POST['email']);
$mail->setPhone($_POST['phone']);
$mail->send();   


?>

And here I'm calling it:

$mail = new Email();
        $mail->setName($_POST['first'], $_POST['last']);
        $mail->setSubject($_POST['subject']);
        $mail->setBody($_POST['body']);
        $mail->setEmail($_POST['email']);
        $mail->setPhone($_POST['phone']);
        $mail->send();

I have the class required once at the top of the script and I don't get any PHP errors when I run it. When I echo $mail->send(); it comes back as true. So could someone please help me understand what is happening here (or why anything isn't happening anymore, to be more exact). The email never comes.

Thanks for everyone's help today, btw.


Like all issues with the core mail() (which your class is based on) function, there are MANY variables that go into what could be causing the email not to deliver. Your host may have limits to how many messages you can queue up, especially when php is mailing it out (which depending on how your server is configured, means it could be dispatching it as user:nobody)

This function is very commonly abused and many shared hosts will impose limitations on its use.

Best suggestion that you run into regularly on SO is to use a mature, complete mailing class solution like PHPMailer. This will allow you to set headers and perform many preparation steps that ensure better deliverability.

The syntax for PHPMailer is very similar to the roll-your-own you've made here, so you should have no trouble adapting it. It also allows you to send attachments and HTML formatted email in a much easier system.


Can you add some info about if, for instance, did you recently recompiled something in your environment?

If this is so, I remember that the email extension, which comes included in PHP 5.x, requires for the unxi email binary to be accesible during the install process.

About the errors, that could be explained by your php.ini configuration. That doesn't mean that there are no warnings.

Hope I was of some help.

Cheers.

A.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜