开发者

How to send e-mails for all members of website with the class?

I need to create a PHP class that would send e-mails to all members of the website. Simple mysql_fetch_array开发者_运维技巧 and loop doesn't fit because my client wants a class and I actually don't know much about classes and what he exactly wants. Can you explain or give me any hints?


Well in general, we can't tell you what your client wants. You'll have to ask him. :) But a few general pointers on sending out bigger numbers of E-Mails.

When putting together an E-Mail class, the most sensitive thing to be aware of, and the most likely reason for it not working, is the number of mails to be sent. mail() is a very slow command for sending mails, and if you have thousands of members, a pure mail() solution is likely to fail, because the run time of a PHP script is usually limited to 30 or 60 seconds.

Check out E-Mailing classes like PHPMailer that can send directly through SMTP, which is way faster.

Also be sure to check out whether the server the mails are sent from has any limit imposed on the number of outgoing E-Mails per interval. If you are using a hosting provider, talk to them first. This is also good because then they know you're not misusing their servers for spam.

Be sure to do test runs before actually starting the sending out to actual members.

Also make sure your outgoing E-Mails look o.k. technically so they don't get spam filtered. See this question for a few pointers.

For basics on Object Oriented Programming, see e.g. the PHP Manual on the subject.

Generally, be very careful when working on a mailing system. Make sure you have checks in place that prevent actual sending to actual members before everything is in order. Imagine how embarrassing it is to have ten "asfsafsd" test E-Mails sent out to each person on the recipient list.

Don't forget that there are paid services available that do this as well. If there is some budget involved, and you would like to use an existing paid solution rather than do it yourself, you can set up another question here on SO.


Well first you should ask your client what he wants if you don't know it. Otherwise you can't proceed. Second, make yourself familiar with Object Oriented Programming especially in PHP.


if can use phpmailer is a class that handle all mail send,

or use a class from phpclasess site (required register)

http://www.phpclasses.org/browse/package/1553.html

the class code :

<? 
/* sendMail Class - by André Cupini - andre@neobiz.com.br */ 
class sendMail 
{ 
    var $to; 
    var $cc; 
    var $bcc; 
    var $subject; 
    var $from; 
    var $headers; 
    var $html; 

    function sendMail()  
    { 
        $this->to       = NULL; 
        $this->cc       = NULL; 
        $this->bcc      = NULL; 
        $this->subject  = NULL; 
        $this->from     = NULL; 
        $this->headers  = NULL;   
        $this->html     = FALSE; 
    } 

    function getParams($params)  
    { 
        $i = 0; 
        foreach ($params as $key => $value) { 
            switch($key) { 
                case 'to': 
                    $this->to       = $value; 
                break; 
                case 'cc': 
                    $this->cc       = $value; 
                break; 
                case 'bcc': 
                    $this->bcc       = $value; 
                break; 
                case 'subject': 
                    $this->subject  = $value; 
                break; 
                case 'from': 
                    $this->from     = $value; 
                break; 
                case 'submitted': 
                    NULL; 
                break; 
                default: 
                    $this->body[$i]["key"]     = str_replace("_", " ", ucWords(strToLower($key))); 
                    $this->body[$i++]["value"] = $value; 
            } 
        } 
    } 

    function setHeaders()  
    { 
        $this->headers = "From: $this->from\r\n"; 
        if($this->html === TRUE) { 
            $this->headers.= "MIME-Version: 1.0\r\n"; 
            $this->headers.= "Content-type: text/html; charset=iso-8859-1\r\n"; 
        } 
        if(!empty($this->cc))  $this->headers.= "Cc: $this->cc\r\n"; 
        if(!empty($this->bcc)) $this->headers.= "Bcc: $this->bcc\r\n"; 
    } 

    function parseBody()  
    { 
        $count = count($this->body); 
        for($i = 0; $i < $count; $i++) { 
            if($this->html) $content.= "<b>"; 
            $content .= $this->body[$i]["key"].': '; 
            if($this->html) $content.= "</b>"; 
            if($this->html) $content .= nl2br($this->body[$i]["value"])."\n"; 
            else $content .= $this->body[$i]["value"]; 
            if($this->html) $content.= "<hr noshade size=1>\n"; 
            else $content.= "\n".str_repeat("-", 80)."\n"; 
        } 
        if($this->html) { 
            $content = " 
            <style> 
                BODY { 
                  font-family: verdana; 
                  font-size: 10; 
                } 
            </style> 
            ".$content; 
        } 
        $this->body = $content; 
    } 

    function send()  
    { 
        if(mail($this->to, $this->subject, $this->body, $this->headers)) return TRUE; 
        else return FALSE; 
    } 

    function set($key, $value)  
    { 
        if($value) $this->$key = $value; 
        else unset($this->$key); 
    } 
} 
?>


Having a class is a pretty vague requirement, as you could simply wrap your code into a Class like this:

class Mailer
{
    public function sendMail()
    {
        // $users = mysql_fetch_array()
        // loop $users
           // mail($user)
    }

}

Requirement met. Granted, it's a bad class and it's likely not, what your client had in mind. I suggest to ask your client to be more specific about the requirements or what he is looking for the class to do exactly and what level of abstraction and flexibility he seeks.

Then again, there is no need to reinvent classes that can send eMail, when there is a plethora readily available. Two very flexible being Zend_Mail and PEAR Mail.


Yup, all above answers are true. I use PHP Mailer all of the time. I personally have an "outbox" table in my database where I queue outgoing messages. Then I have a CRON job go through and send chunks out at a time. Not only does this give you the control over your rate of sending but if you "mess up" and send something wrong, you can quickly go into the database and clear them all out before the cron comes around. I've even implemented priorities in my bigger projects so that important emails will go out before things like a newsletter.

Good Luck!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜