0

I'm using PHP mail() function for my emails, now I want to add new things but don't know how to deal with that..

My basic PHP Mail class that I've created is the following :

<?php

class Send_Mail {

    private $to = array();
    private $subject = null;
    private $from = null;
    private $replyTo = null;
    private $type = self::TEXT;

    const HTML = 0;
    const TEXT = 1;

    public function __construct() {
    }

    public function setTo($array) {
        $this->to = $array;
    }

    public function setType($type) {
        if($type == self::HTML || $type == self::TEXT) {
            $this->type = $type;
        }
    }

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

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

    public function setFrom($from) {
        $this->from = $from;
    }

    public function send() {

        if  (   $this->to == array()    || 
                $this->from == null     ||
                $this->message == null
            ) {
                trigger_error("The email can't be sent ! One of the mandatory fields at least isn't set !");
                return false;
        }
        else {

            $this->to       = implode(',', $this->to);

            $this->subject = ($this->subject == null) ? "" : $this->subject;

            $headers    = array();
            $headers[]  = "MIME-Version: 1.0" . "\r\n";

            if($this->type == self::HTML) {
                $headers[]  = "Content-type: text/html; charset=UTF-8" . "\r\n";
            } else {
                $headers[]  = "Content-type: text/plain; charset=UTF-8" . "\r\n";
            }

            $headers[]  = "To: " . $this->to . "\r\n";
            $headers[]  = "From: " . $this->from . "\r\n";


            $headers[]  = "Reply-To: " . $this->from . "\r\n";

            return mail($this->to, $this->subject, $this->message, implode('', $headers));
        }
    }

}

*/
?>

So basically I can't send attachments.. I also want to add multiple BCC and multiple CC support.

Along with the fact that lines should not be longer than 70 characters as said in PHP documentation. How can I deal with that knowing that I can break a HTML tag if I use wordwrap? :(

  • I would look for a nice class that does it – Popnoodles Jan 10 '13 at 13:04
  • Don't use the `mail()` function, especially not for complex tasks like attachments. Use a decent mailer class instead like phpMailer. See also this answer: http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail/12302354#12302354 – SDC Jan 10 '13 at 15:46

1 Answers1

0

Provide emails with attachments is not this easy bacause you need to implement multy part bodys.

Better use a libary for this problem. For example:

http://framework.zend.com/manual/1.12/en/zend.mail.attachments.html

or

https://pear.php.net/manual/de/package.mail.mail-mime.example.php

otherwise you have to write it by your self

for attachments, have a look at this

http://www.shayanderson.com/php/simple-php-mail-class-with-attachment.htm

For additional email headers like CC or BCC read this

http://www.htmlite.com/php029.php

GreenRover
  • 1,486
  • 1
  • 14
  • 33
  • I don't like frameworks, Zend gives you a huge amount of possibilities but it doesn't target one specific kind of websites so it leads to a big memory usage. Using it on huge websites will certainly lead to memory problems but that's another issue ! – user1777616 Jan 10 '13 at 14:20
  • I still advice you to use a framework (in case of zend you only have to use the parts you need, to hte whole MVC stuff). But i advaned my answer with some tips to advacne you own class. – GreenRover Jan 10 '13 at 15:20