WebsiteBaker Community Forum

General Community => Global WebsiteBaker 2.8.x discussion => Topic started by: sharmpro on June 13, 2008, 10:15:06 AM

Title: Extending wb->mail to attach files
Post by: sharmpro on June 13, 2008, 10:15:06 AM
Hi,
attaching files to an email is an easy task, just will be nice to have it implemented...

Here my solution.... (in red the changes from the standard wb-mail code)

class wbx extends wb
{
    function wbx() {
    }
// Validate send email
    function mailx($fromaddress, $toaddress, $subject, $message, $fromname='',$file_attached='') {
        /*
            INTEGRATED OPEN SOURCE PHPMAILER CLASS FOR SMTP SUPPORT AND MORE
            SOME SERVICE PROVIDERS DO NOT SUPPORT SENDING MAIL VIA PHP AS IT DOES NOT PROVIDE SMTP AUTHENTICATION
            NEW WBMAILER CLASS IS ABLE TO SEND OUT MESSAGES USING SMTP WHICH RESOLVE THESE ISSUE (C. Sommer)

            NOTE:
            To use SMTP for sending out mails, you have to specify the SMTP host of your domain
            via the Settings panel in the backend of WebsiteBaker
        */

        $fromaddress = preg_replace('/[\r\n]/', '', $fromaddress);
        $toaddress = preg_replace('/[\r\n]/', '', $toaddress);
        $subject = preg_replace('/[\r\n]/', '', $subject);
        $message = preg_replace('/[\r\n]/', '<br \>', $message);
       
        // create PHPMailer object and define default settings
        $myMail = new wbmailer();

        // set user defined from address
        if ($fromaddress!='') {
            if($fromname!='') $myMail->FromName = $fromname;         // FROM-NAME
            $myMail->From = $fromaddress;                            // FROM:
            $myMail->AddReplyTo($fromaddress);                       // REPLY TO:
        }
       
        // define recepient and information to send out
        $myMail->AddAddress($toaddress);                            // TO:
        $myMail->Subject = $subject;                                // SUBJECT
        $myMail->Body = $message;                                   // CONTENT (HTML)
        $myMail->AltBody = strip_tags($message);                    // CONTENT (TEXT)

        if ($file_attached!='' ) {
            if (is_array($file_attached)) {
                foreach($file_attached as $k => $v) {
                    $myMail->AddAttachment($k, $v);                  // ATTACHMENT (FILE)
                }
            } else {
                $myMail->AddAttachment(dirname($file_attached), basename($file_attached));                  // ATTACHMENT (FILE)
            }        
        }
        // check if there are any send mail errors, otherwise say successful
        if (!$myMail->Send()) {
            return false;
        } else {
            return true;
        }
    }
}