开发者

Email class PHP

I'm new to OOP and could use some help. My class is not working:

class Email {

  private $to = 'shummel@...';

  public $subject;
  public $body;

  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");
   $sent = mail($this->to, $this->subject, $this->body, $this->headers);
   开发者_如何学JAVAreturn $sent;
  }

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

 }

And here I am calling it:

$mail = new Email();
$mail = new Email;
$mail->subject($_POST['subject']);
$mail->body($_POST['body']);
$mail->send();

I've done print_r on the $_POST and the values are there. There are no problems with our mail server. So I'm not sure what I'm doing wrong. I'd appreciate some help. Thanks.


I recommend using PHPMailer, which is a free and mature mailer class for PHP.


$this->headers is not defined. Turn on your error reporting.


and I use Zend_Mail , its pretty powerful ,

http://framework.zend.com

btw , you can use it alone : http://epic.codeutopia.net/pack/

http://codeutopia.net/blog/2008/11/14/improved-zend-framework-package-maker/


There is no subject and body methods. They are attributes.

$mail->subject = $_POST['subject'];
$mail->body = $_POST['body'];

And of course, never trust data from $_POST -- you need some kind of escaping.


$mail = new Email(); 
$mail->subject = $_POST['subject'=; 
$mail->body = $_POST['body']; 
$mail->send(); 

and if you want use the methoted you are useing then try this

class Email {   

  private $to = 'shummel@...';   

  public $subject;   
  public $body;   

  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");   
   $sent = mail($this->to, $this->subject, $this->body, $this->headers);   
   return $sent;   
  }   

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


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

  public function SetBody($body) {   
   $this->body= $body;   
  }

 }

$mail = new Email;    
$mail->SetSubject($_POST['subject']);    
$mail->SetBody($_POST['body']);    
$mail->send(); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜