• 85 Vote(s) - 2.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
سورس کد فرستادن ایمیل به همراه فایل پیوست (پی اچ پی)
#1
درود
سورس زیر که با زبان php نوشته شده است یاد میگیرید که چگونه ایمیل بفرستید و همراش حتی فایل پیوست کنید.
Email with attachment
Code:
Email with attachment<?php
$fileatt = ""; // Path to the file                  
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment $email_from = ""; // Who the email is from
$email_subject = ""; // The Subject of the email
$email_message = ""; // Message that the email has in it $email_to = ""; // Who the email is too $headers = "From: ".$email_from; $semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
$headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\""; $email_message .= "This is a multi-part message in MIME format.\n\n" .
                "--{$mime_boundary}\n" .
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
               "Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n"; /* First File*/
$fileatt = ""; // Path to the file                  
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment $file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                 $data . "\n\n" .
                  "--{$mime_boundary}--\n";
unset($data)
unset($file)
unset($fileatt)
unset($fileatt_type)
unset($fileatt_name)
/* Second File */ $fileatt = ""; // Path to the file                  
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment $file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" .
                  "Content-Type: {$fileatt_type};\n" .
                  " name=\"{$fileatt_name}\"\n" .
                  //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  "Content-Transfer-Encoding: base64\n\n" .
                 $data . "\n\n" .
                  "--{$mime_boundary}--\n";
unset($data)
unset($file)
unset($fileatt)
unset($fileatt_type)
unset($fileatt_name)
/* End of File Config */ // To add more files just copy the file section again, but make sure they are all one after the other! If they are not it will not work!
$ok = @mail($email_to, $email_subject, $email_message, $headers); if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
وب سایت تحلیل ارز و دلار
http://tradingview.website
برای دریافت قیمت ها اخبار اقتصادی دلار و ارز حتما وارد کانال تلگرام شوید .
https://t.me/Tradegolds
  Reply
#2
Code:
//**************************************
    // 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-&gt;setEmailToAddress("backie@testtest.com");
    $objMail-&gt;setEmailReplyTo("backie@ymail.com");
    $objMail-&gt;setEmailFromAddress("backie@gmail.com");
    $objMail-&gt;setEmailFromName("Iain Cambridge");
    $objMail-&gt;setEmailSubject("Example Code");
    $objMail-&gt;setEmailBody("This is an example email");
    $objMail-&gt;setEmailType(Email::BODY_HTML);
    for a html email.
    or
    $objMail-&gt;setEmailType(Email::BODY_TEXT);
    plain text email
    $objMail-&gt;addAttachment("fileone.txt"); // You can add as many as you want.
    $objMail-&gt;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;
      }
     }
    }
    ?>
وب سایت تحلیل ارز و دلار
http://tradingview.website
برای دریافت قیمت ها اخبار اقتصادی دلار و ارز حتما وارد کانال تلگرام شوید .
https://t.me/Tradegolds
  Reply
#3
مچکرم
  Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  سورس کد فرم ثبت نام کاربر (PHP) Amin_Mansouri 3 9,660 11-10-2014، 09:11 PM
Last Post: farshadm
  سورس کد ساخت اعداد تصادفی (پی اچ پی) Amin_Mansouri 2 5,624 09-11-2014، 07:22 AM
Last Post: mahfelsoft
  سورس کد فاکتوریل php Amin_Mansouri 1 5,802 06-12-2014، 12:22 AM
Last Post: Amin_Mansouri
  سورس کد پرینت اعداد و حروف به صورت وارونه Amin_Mansouri 0 2,401 06-09-2014، 11:10 AM
Last Post: Amin_Mansouri
  سورس کد پرینت میانگین 5 عدد در PHP Amin_Mansouri 0 2,705 06-08-2014، 11:51 PM
Last Post: Amin_Mansouri
  سورس کد پرینت تعداد مقسوم علیه یک عدد PHP Amin_Mansouri 0 3,790 06-08-2014، 11:36 PM
Last Post: Amin_Mansouri
  اپلود فایل Ghoghnus 7 8,176 01-28-2013، 11:32 PM
Last Post: MarMar
  سورس کد صفحه لاگین و ثبت نام کاربر (php) Amin_Mansouri 0 7,197 11-30-2012، 12:15 PM
Last Post: Amin_Mansouri
  سورس کد حذف ویرایش و اضافه کردن در دیتابیس (ساده) (php( Amin_Mansouri 0 7,637 09-04-2012، 11:37 PM
Last Post: Amin_Mansouri
  اپلود فایل با نام رندم امیر 0 2,384 08-05-2012، 12:34 PM
Last Post: امیر

Forum Jump:


Users browsing this thread: 1 Guest(s)