static function newMessageEmail($message)
 {
     $mail = new SCEmail();
     $mail->bcc = $mail->loadMessageEmailRecipients($message);
     //$mail->from = SC_MESSAGE_EMAIL_FROM . "<" . str_replace(":boardid", $message->boardid, SC_MESSAGE_EMAIL_FROM_ADDRESS) . ">";
     $mail->to = "";
     $mail->author = $message->author();
     $thread = $message->threadid ? new SCThread($message->threadid) : $message;
     $mail->from = SC_MESSAGE_EMAIL_FROM . "<" . $thread->emailAddress() . ">";
     //$mail->body = "Posted By: " . $mail->author->displayname . "\n\n" .str_replace("<br/>", "\n", $message->text); //mail body
     $mail->body = "Posted By: " . $mail->author->displayname . "\n\n" . SCPartial::renderToString("message/" . $message->type . "_body", array("message" => $message, "linebreak" => "\n"));
     $mail->subject = $thread->subject;
     //subject
     $mail->headers = "From: " . $mail->from . "\r\n" . "Bcc: " . $mail->bcc . "\r\n" . "Reply-To: " . $thread->emailAddress() . "\r\n" . "X-Mailer: PHP/" . phpversion();
     return $mail;
 }
 public function sendEmail()
 {
     if (!$this->id) {
         throw new InviteException("You can only send an email for an existing invitation");
     }
     if ($this->accepted) {
         throw new InviteException("You can not re-send an accepted invitation");
     }
     $invite_email = SCEmail::newInviteEmail($this);
     $invite_email->sendEmail();
     return $this;
 }
 public function create()
 {
     //$userid, $subject, $text, $source=false) {
     if (!$this->author() || !$this->author()->userid) {
         throw new ThreadException("You need a valid userid to create a thread");
     }
     if (!$this->boardid) {
         throw new ThreadException("You need a valid boardid to create a thread");
     }
     if (!$this->subject) {
         throw new ThreadException("You need a subject to create a thread");
     }
     if (!$this->text && !$this->media) {
         throw new ThreadException("You need a valid message to create a thread");
     }
     if (!$this->author()->isMemberOf($this->boardid)) {
         throw new ThreadException("You may only create threads for boards you belong to", 401);
     }
     if ($this->type == "image") {
         $asset = new SCAsset($this->author()->userid, $this->media);
         $this->media = $asset->hash;
     }
     //$sql = "INSERT INTO messages (msg_date, msg_author, msg_subject, msg_text, msg_board_id" . ($source ? ", msg_source" : "") . ") VALUES('".SC::dbDate()."', $userid, '".SC::dbString($subject) ."', '" .SC::dbString($text) ."', " . $this->boardid  . ($source ? ", '" . SC::dbString($source) . "'" : "") . ")";
     $db = new SCDB();
     //$db->query($sql);
     $insert_array = array("msg_date" => SC::dbDate(), "msg_author" => SC::dbString($this->author()->userid, true), "msg_subject" => SC::dbString($this->subject, true), "msg_text" => SC::dbString($this->text, true), "msg_board_id" => $this->boardid, "msg_source" => SC::dbString($this->source, true), "msg_type" => SC::dbString($this->type, true));
     if ($this->media) {
         $insert_array["msg_media"] = SC::dbString($this->media, true);
     }
     if ($this->caption) {
         $insert_array["msg_media_caption"] = SC::dbString($this->caption, true);
     }
     $db->insertFromArray($insert_array, "messages");
     $newthread = mysql_insert_id($db->conn);
     if ($newthread) {
         /*
         			if(!SC::isLocal()) {
         				$user = SCUser::newFromId($userid);
         				$Name = "Switchcomb"; //senders name
         				$email = $this->boardid."@boards.switchcomb.com"; //senders e-mail adress
         				$recipient = ""; //recipient
         				$mail_body = "Posted By: " . $user->displayname . "\n\n" .str_replace("<br/>", "\n", $text); //mail body
         				$subject = "[" . $newthread . "] $subject"; //subject
         				$header = "From: ". $Name . " <" . $email . ">\r\nBcc:" . SC::emailList() ."\r\n"; //optional headerfields
         
         				mail($recipient, $subject, $mail_body, $header); //mail command :)
         			}
         */
         $thread = new SCThread($newthread);
         $this->fromArray($thread->toArray());
         try {
             $messageMail = SCEmail::newMessageEmail($thread);
             $messageMail->sendEmail();
         } catch (Exception $ex) {
         }
         return $this;
     } else {
         throw new ThreadException(mysql_error($db->conn));
     }
 }
 public function create()
 {
     /*
     if((!$this->author || !$this->author->existing) && $this->authorid) {
       $this->author = new SCUser($this->authorid);
     }
     */
     if (!$this->author() || !$this->author()->userid) {
         throw new MessageException("You need a valid userid to create a message");
     }
     if (!$this->threadid) {
         throw new MessageException("You need a valid threadid to create a message");
     }
     if (!$this->boardid) {
         throw new MessageException("You need a valid boardid to create a message");
     }
     if (!$this->text && !$this->media) {
         throw new MessageException("You need valid message content to create a message");
     }
     if ($this->type == "image") {
         $asset = new SCAsset($this->author()->userid, $this->media);
         $this->media = $asset->hash;
     }
     //$sql = "INSERT INTO messages (msg_date, msg_author, msg_subject, msg_text, msg_board_id" . ($source ? ", msg_source" : "") . ") VALUES('".SC::dbDate()."', $userid, '".SC::dbString($subject) ."', '" .SC::dbString($text) ."', " . $this->boardid  . ($source ? ", '" . SC::dbString($source) . "'" : "") . ")";
     $db = new SCDB();
     //$db->query($sql);
     $insert_array = array("msg_date" => SC::dbDate(), "msg_author" => SC::dbString($this->author()->userid, true), "msg_subject" => SC::dbString($this->subject, true), "msg_text" => SC::dbString($this->text, true), "msg_thread" => $this->threadid, "msg_board_id" => $this->boardid, "msg_source" => SC::dbString($this->source, true), "msg_type" => SC::dbString($this->type, true));
     if ($this->media) {
         $insert_array["msg_media"] = SC::dbString($this->media, true);
     }
     if ($this->caption) {
         $insert_array["msg_media_caption"] = SC::dbString($this->caption, true);
     }
     $db->insertFromArray($insert_array, "messages");
     $newmessage = mysql_insert_id($db->conn);
     if ($newmessage) {
         $message = new SCMessage($newmessage);
         $this->fromArray($message->toArray());
         try {
             $messageMail = SCEmail::newMessageEmail($message);
             $messageMail->sendEmail();
         } catch (Exception $ex) {
         }
         return $this;
     } else {
         throw new MessageException(mysql_error($db->conn));
     }
 }