Utklippstavle
Valgmuligheter
ZtigO sendte inn denne php-koden 23.07.2009 kl. 13:02.
<?php /** * nerdMail class * @author Stig Magnus Halvorsen (ZtigO) * @version 1.0.2 * * http://nerdvar.com/ * * A class using PHPs mail() function to send * emails as text and/or HTML with or without * attachments. Highly customizeable headers * and with a emailadress validator and anti * email injection function. */ class nerdMail { /** * define the required variables */ public $text; public $html; public $subject; public $sender; public $returnPath; /** * The list of accepted encoding of attachments * default is BASE64 * Must be uppercase! */ /** * The list of not overrideable headers * To prevent different unwanted results * The standards shouldn't be removed * Must be uppercase! */ /** * function $this->parseMail() or nerdMail::parseMail() * Based upon Hellkeepa's Val_email() function * Validates the email with a regular expression, then * checks if the mail server exists using checkdnsrr() * * @param string $email * @return boolean */ $RegExp = "/^[a-z][\\w\\pL\\.-]*[a-z0-9]@[a-z0-9][\\w\\pL\\.-]*[a-z0-9]\\.[a-z][a-z\\.]*[a-z]\\z/ui"; return true; } } return false; } /** function $this->antiInjection or nerdMail::antiInjection * looks for email injection attempts in $field by checking * for line breaks and horizontal tabs * * @param string|int $field * @return boolean */ return true; } else { return false; } } /** function $this->addMail * type: to, cc or bcc * email: address to add * parse: true -> validate $email * * Use it to populate the list of recipients * to for regular recievers, cc for carbon copy * and bcc for blind carbon copy * * @param "to"|"cc"|"bcc" $type["to"] * @param string $email * @param boolean $parse[false] * @return boolean */ public function addMail($type, $email, $parse = false) { return false; } if ($parse) { if (!$this->parseMail($email)) { return false; } } else { if ($this->antiInjection($email)) { return $email; } } switch ($type) { case "to": $this->receivers[] = $email; break; case "cc": $this->Cc[] = $email; break; case "bcc": $this->receivers[] = $email; break; default: $this->receivers[] = $email; } return true; } /** function $this->addHeader * Use it to add extra headers or to * override the nerdMail default ones * * Examples: * $this->addHeader("List-Unsubscribe", "<http://nerdvar.com/unsubscripe.php>"); * $this->addHeader("Message-Id: 123xxx@nerdvar.com"); * * @param string $key * @param boolean $val[false] * @return boolean */ public function addHeader ($key, $val = false) { return false; } /** Check for injectionattempt */ if ($this->antiInjection($key) or $this->antiInjection($val)) { return false; } if ($val == false) { $key = $val[0]; $val = $val[1]; } else { } } $this->headers["keys"][] = $key; $this->headers["values"][] = $val; return true; } /** function $this->attach * filePath: http://example.com/myfile.jpg, myfile.txt, etc * contenttype: image/jpeg, plain/text, etc.. * encoding: BASE64, BIT7, etc.. Must be in $enctypes array * * Adds and validates file to attach in the attachment list * * @param string $filePath * @param string $contenttype["OCTET"] * @param string $encoding["BASE64"] * @return boolean */ public function attach ($filePath, $contenttype = "OCTET", $encoding = "BASE64") { return false; } $contenttype = "OCTET"; } $encoding = "BASE64"; } if ($data) { $thisEnc = "base64"; } $this->attachments["encoding"][] = $thisEnc; $this->attachments["cType"][] = "text/plain; charset=UTF-8"; } elseif ($contenttype == OCTET) { $this->attachments["cType"][] = "application/octet-stream"; } else { $this->attachments["cType"][] = $contenttype; } return true; } else { return false; } } /** function $this->send * This completes the mailing by putting it all togheter and * sending it out. Returns false on failure and fails if it's * missing some required vars * * @return mixed */ public function send() { $i = 0; /** Define the multipart email hdr info */ /** Check wether it's any attachments or not and sets multitype */ $multiType = "mixed"; $attach = true; } else { $multiType = "alternative"; $attach = false; } /** Verify that the required variables are set */ return false; } /** Validate Subject */ $this->subject = "(No subject)"; } /** Add the receivers */ $sendData["receivers"] .= $this->receivers[$i]; $i++; $sendData["receivers"] .= ", "; } } $i = 0; /** Add the Carbon Copy receivers (CC) */ $sendData["Cc"] .= $this->Cc[$i]; $i++; $sendData["Cc"] .= ", "; } } $i = 0; /** Add the Blind Carbon Copy receivers */ $sendData["Bcc"] .= $this->Bcc[$i]; $i++; $sendData["Bcc"] .= ", "; } } $i = 0; /** Add the Headers **/ $headers .= "MIME-Version: 1.0\r\n"; } $headers .= "Content-Type: multipart/$multiType; boundary=\"$boundary1\"\r\n"; $headers .= "From: ".$this->sender."\r\n"; $headers .= "Reply-To: ".$this->sender."\r\n"; } /** Make sure the header isn't in the blocked list */ $headers .= $this->headers["keys"][$i].": ".$this->headers["values"][$i]."\r\n"; } $i++; } $i = 0; } if ($sendData["Cc"]) { $headers .= "Cc: ".$sendData["Cc"]."\r\n"; } if ($sendData["Bc"]) { $headers .= "Bcc: ".$sendData["Cc"]."\r\n"; } $headers .= "\n"; /** Add the mail body */ if (!$attach) { $body = "MIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary=\"$boundary1\"\r\n\r\n"; $body .= "--$boundary1\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 7bit\n\n".$this->text."\n\n"; } $body .= "--$boundary1\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: 7bit\n\n".$this->html."\n\n"; } } else { $body = "--$boundary1\nContent-Type: multipart/alternative; boundary=\"$boundary2\"\r\n\r\n"; $body .= "--$boundary2\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 7bit\n\n".$this->text."\n\n"; } $body .= "--$boundary2\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: 7bit\n\n".$this->html."\n\n"; } $body .= "--$boundary2--\n\n"; /** Add attachments */ $body .= "--$boundary1\nContent-Type: ".$this->attachments["cType"][$i]."; name=\"".$this->attachments["name"][$i]."\"\nContent-Transfer-Encoding: ".$this->attachments["encoding"][$i]."\nContent-Disposition: attachment; filename=\"".$this->attachments["name"][$i]."\"\n\n"; $body .= $this->attachments["data"][$i]; $i++; } $i = 0; } $body .= "--$boundary1--"; /** Send the final mail */ } else { } } } /** checkdnsrr() FUNCTION FOR WINDOWS SERVER WITH PHP VERSIONS LOWER THAN 5.3 * Checks for existence and creates the function if it doesn't, parameter * data is validated to avoid shell injection * * It's being used by the $nerdMail->pasreMail() function * * @param string $hostName * @param string $recType[""] * @return boolean */ /** List of accepted recTypes */ $accepted = array('A', 'MX', 'NS', 'SOA', 'PTR', 'CNAME', 'AAAA', 'A6', 'SRV', 'NAPTR', 'TXT', 'ANY'); global $accepted; /** validate the recType */ /** Validate domain/host name */ foreach ($result as $line) { return true; } } } return false; } return false; } } ?>
