06-19-2012، 10:59 AM
کد:
//**************************************
// Name: Send Email with Attachments in HTML
// Description:Email class to do simple mail and attachments (Multiple) and HTML emails sending using mail() function.
Usage is in Input section. Any questions just contact me.
// By: Iain Cambridge
//
//
// Inputs:$objMail = new Email(Email::DEBUG_HTML);
$objMail = new Email();
for html debugging info
or
$objMail = new Email(Email::DEBUG_ERROR);
for it to trigger a catchable error.
or
$objMail = new Email(Email::DEBUG_EXCEP);
for it to trigger exceptions for debugging.
$objMail->setEmailToAddress("backie@testtest.com");
$objMail->setEmailReplyTo("backie@ymail.com");
$objMail->setEmailFromAddress("backie@gmail.com");
$objMail->setEmailFromName("Iain Cambridge");
$objMail->setEmailSubject("Example Code");
$objMail->setEmailBody("This is an example email");
$objMail->setEmailType(Email::BODY_HTML);
for a html email.
or
$objMail->setEmailType(Email::BODY_TEXT);
plain text email
$objMail->addAttachment("fileone.txt"); // You can add as many as you want.
$objMail->sendMail();
//
// Returns:None
//
//Assumes:None
//
//**************************************
<?php
/*\
* Author : Iain Cambridge
* Class Name : Email
* Version: 1.0
* Purpose: To handle the sending of emails.
\*/
class Email {
constDEBUG_ERROR = 1;
constDEBUG_EXCEP = 2;
constDEBUG_HTML = 3;
constBODY_HTML = 1;
constBODY_TEXT = 0;
private $EmailToAddress;
private $EmailFromAddress;
private $DebugMode;
private $EmailSubject;
private $EmailBody;
private $EmailFromName;
private $EmailType;
private $EmailAttachments;
private $EmailReplyTo;
private $EmailBoundry;
public function __construct($DebugMode = 3){
$this->DebugMode = $DebugMode;
}
/*\
* @Function : doDebug()
*: Handles the debugging data.
* @Param: $DebugMessage
*: The error message that is thrown, printed
* : or triggered.
* @Return: void
\*/
private function doDebug($DebugMessage){
if ((empty($this->DebugMode)) || ($this->DebugMode != 0)){
trigger_error($DebugMessage);
}
elseif($this->DebugMode == 1){
throw new Exception($DebugMessage);
}
elseif($this->DebugMode == 2){
// HTML removed due to Planet Source Code's system
print "{$DebugMessage}\r\n";
}
}
/*\
* @Function : setEmailToAddress
*: Sets the email address we are sending to.
* @Param: $EmailAddress
*: For mentioned email address.
* @Return: bool
\*/
public function setEmailToAddress($EmailAddress){
if ( (!is_string($EmailAddress)) || (!preg_match("/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/",$EmailAddress)) ){
$this->doDebug("Invalid to email address.");
return false;
}
else
{
$this->EmailToAddress = $EmailAddress;
return true;
}
}
/*\
* @Function : setEmailReplyTo()
*: Set the email address that replies have
* : to goto.
* @Param: $EmailAddress
*: Formentioned email address.
* @Return: bool
\*/
public function setEmailReplyTo($EmailAddress){
if ( (!is_string($EmailAddress)) || (!preg_match("/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/",$EmailAddress)) ){
$this->doDebug("Invalid reply to email address.");
return false;
}
else
{
$this->EmailReplyTo = $EmailAddress;
return true;
}
}
/*\
* @Function : setEmailFromAddress
*: Sets the address which the email is
* : coming from.
* @Param: $EmailAddress
*: Formentioned email address.
* @Return: bool
\*/
public function setEmailFromAddress($EmailAddress){
if ( (!is_string($EmailAddress)) || (!preg_match("/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/",$EmailAddress)) ){
$this->doDebug("Invalid from email address.");
return false;
}
else
{
$this->EmailFromAddress = $EmailAddress;
return true;
}
}
/*\
* @Function : setEmailSubject
*: Sets the subject for the email.
* @Param: $EmailSubject
* : The subject.
* @Return: bool
\*/
public function setEmailSubject($EmailSubject){
if ( (!is_string($EmailSubject)) ){
$this->doDebug("Invalid email subject.");
return false;
}
else
{
$this->EmailSubject = $EmailSubject;
return true;
}
}
/*\
* @Function : setEmailBody
*: Sets the message for the email.
* @Param: $EmailBody
*: String which we are gonna send.
* @Return: bool
\*/
public function setEmailBody($EmailBody){
if (!is_string($EmailBody)) {
$this->doDebug("Invalid email body.");
return false;
}
else
{
$this->EmailBody = $EmailBody;
return true;
}
}
/*\
* @Function : setEmailType
* : Sets the type of email is send.
* @Param: $EmailType
*: 0 is plain text, 1 is html.
* @Return: bool
\*/
public function setEmailType($EmailType){
// EmailType 0 is text
// EmailType 1 is html
if (!is_int($EmailType)){
$this->doDebug("Invalid email type.");
return false;
}
else
{
$this->EmailType = $EmailType;
return true;
}
}
/*\
* @Function : setEmailFromName
*: Sets the name of the person sending the email.
* @Param: $EmailFromName
*: The Name
* @Return: bool
\*/
public function setEmailFromName($EmailFromName){
if (!is_string($EmailFromName)) {
$this->doDebug("Invalid email from name.");
return false;
}
else
{
$this->EmailFromName = $EmailFromName;
return true;
}
}
/*\
* @Function : setDebugMode
*: Sets the mode in which debuging is done.
* @Param: $DebugMode
*: 1 is trigger an error
*: 2 is throw exceptions
*: 3 is show html page
* @Return: bool
\*/
public function setDebugMode($DebugMode){
// Debug mode 1 is trigger error
// Debug mode 2 is throw exceptions
// Debug mode 3 is show html page
if( ($DebugMode != 1) && ($DebugMode != 2) && ($DebugMode != 3) ){
$this->doDebug("Invalid Debug Mode!?!?!?!");
return false;
}
else
{
$this->DebugMode = $DebugMode;
return true;
}
}
/*\
* @Function : addAttachment
*: Adds a filename to an array
*: which are to be attached to
*: the email.
* @Param: $Filename
*: The file name
* @Return: bool
\*/
public function addAttachment($Filename){
if ( (!is_string($Filename)) || (!file_exists($Filename)) ){
$this->doDebug("Invalid attachment.");
return false;
}
else
{
if (!is_array($this->EmailAttachments)){
$this->EmailAttachments = array();
}
array_push($this->EmailAttachments,$Filename);
return true;
}
}
/*\
* @Function : createHeader
*: Creates the header
* @Param: none
* @Return: String
*: The header.
\*/
private function createHeader(){
if ((empty($this->EmailFromAddress)) || (empty($this->EmailFromName))){
$this->doDebug("Not all required info was given");
return false;
}
else
{
$unique_sep = "php-".md5(date('r', time()));
$EmailHeader = "From: {$this->EmailFromName} <{$this->EmailFromAddress}>\r\n";
if (!empty($this->EmailReplyTo)){
$EmailHeader .= "Reply-To: {$this->EmailReplyTo}\r\n";
}
else
{
$EmailHeader .= "Reply-To: {$this->EmailFromAddress}\r\n";
}
$EmailHeader .= "MIME-Version: 1.0\r\n";
$EmailHeader .= "Content-Type: multipart/mixed;boundary=\"$unique_sep\";\r\n";
$EmailHeader .= "charset=\"iso-8859-1\"\r\n";
$EmailHeader .= "Content-Transfer-Encoding:7bit\r\n\r\n";
$EmailHeader .= "If you are reading this, then your e-mail client does not support MIME.\r\n";
$EmailHeader .= "\r\n";
$EmailHeader .= "--$unique_sep\r\n";
if ((empty($this->EmailType)) || ($this->EmailType == 0)){
$EmailHeader .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
}
else
{
$EmailHeader .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
}
$EmailHeader .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$EmailHeader .= $this->EmailBody."\r\n\r\n";
if (!empty($this->EmailAttachments)){
foreach ($this->EmailAttachments as $Attachment){
$EmailHeader .= "--{$unique_sep}\r\n";
$EmailHeader .= "Content-Type: ". mime_content_type($Attachment)."; name=\"".basename($Attachment)."\"\r\n";
$EmailHeader .= "Content-Transfer-Encoding: base64\r\n";
$EmailHeader .= "Content-Disposition: attachment\r\n\r\n";
$EmailHeader .= chunk_split(base64_encode(file_get_contents($Attachment)))."\r\n";
}
}
// End of adding EmailAttachments.
return $EmailHeader;
}
}
/*\
* @Function : sendMail
*: Sends the email.
* @Return: bool
\*/
public function sendMail(){
$EmailHeader = $this->createHeader();
if (!mail($this->EmailToAddress,$this->EmailSubject,$this->EmailBody,$EmailHeader)){
$this->doDebug("Email fail to send");
return false;
}
else
{
return true;
}
}
}
?>
گروه دور همی پارسی کدرز
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg