Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ขอความช่วยเหลือครับ PHP ส่งเมลแนบไฟล์ด้วยครับ ช่วยด้วยครับ ไม่นั้นไม่จบแน่ แก้โค้ดให้หน่อยครับติดอันเดียว



 

ขอความช่วยเหลือครับ PHP ส่งเมลแนบไฟล์ด้วยครับ ช่วยด้วยครับ ไม่นั้นไม่จบแน่ แก้โค้ดให้หน่อยครับติดอันเดียว

 



Topic : 105914



โพสกระทู้ ( 4 )
บทความ ( 0 )



สถานะออฟไลน์




ขอความช่วยเหลือครับ PHP ส่งเมลแนบไฟล์ด้วยครับ ช่วยด้วยครับ ไม่นั้นไม่จบแน่ แก้โค้ดให้หน่อยครับติดอันเดียว



Tag : PHP, HTML/CSS, JavaScript, CakePHP, JAVA







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-02-17 14:21:13 By : songpolzaa View : 1184 Reply : 7
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ใช้ PHPMailer ดีกว่าครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-17 14:22:06 By : mr.win
 


 

No. 2



โพสกระทู้ ( 4 )
บทความ ( 0 )



สถานะออฟไลน์


ช่วยหน่อยได้ไหมครับ ผมหมดปัญญาแล้ว
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-17 14:35:52 By : songpolzaa
 

 

No. 3



โพสกระทู้ ( 256 )
บทความ ( 0 )



สถานะออฟไลน์


ผมเจอมาแล้วตอนส่งเมล์พร้อมกับแนบไฟล์เหมือนจะง่ายนะ ปวดหัวชิบเอาไลบรารี่นี้ไปใช้ได้แน่นอน ผมใช้และโอเคเลย ส่งเข้า hotmail gmail outlook ได้ไม่มีปัญหา ลองดูนะงับมีสองไฟล์ คือ emailexception.php และ mail.php เซฟเอาไปและจากนั้นทำการ include mail.php มาใช้ และผมก็เอาวิธีใช้ใส่ไว้ให้แล้ว

ตั้งชื่อว่า emailexception.php
<?php

class EmailException extends Exception
{
	/**
	 * Constructor
	 *	 	
	 * @param string $mesasge
	 * @param integer $code
	 */	 
	
	public function __construct($message = FALSE, $code = FALSE)
	{
		$this->message = $message;
		$this->code = $code;
	}
}

?>



ตั้งชื่อว่า mail.php
<?php

/**
 * @package Mail
 */ 

/**
 * @ignore
 */ 

require_once(@$cfg['library_dir']."emailexception.php");

define("BOUNDARY", "--".md5(rand()));

/**
 * Composite Mail class is able to send multiple email with attachment using PHP::mail()
 * 
 * @version 1.13
 * @uses EmailException
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 * @author Michal "Techi" Vrchota <[email protected]>
 * @category Mail
 * @package Mail 
 *
 */

class CMail
{

	/**
	 * @var string $from your email	
	 */
	 
	public $from;

	/**
	 * @var string $fromName your name	
	 */

	public $fromName;
	
	/**
	 * @var string $to target email
	 */	 	
	
	public $to;
	
	/**
	 * @var string $cc carbon copy
	 */	 	
	
	public $cc;

	/**
	 * @var string $bcc blind carbon copy
	 */	 	


	public $bcc;
	
	/**
	 * @var string $subject email subject
	 */	 	
	
	public $subject;
	
	/**
	 * @var integer $priority email priority 1-5 (3 standart)
	 */	 	
	
	public $priority;
	
	/**
	 * @var string $returnPath override $to to this email for reply address
	 */	 	
	
	public $returnPath;
	
	/**
	 * @var bool $notify send back notification about reading
	 */	 	
	
	public $notify;
	
	/**
	 * @var string $message message text
	 */	 	
	
	public $message;
	
	/**
	 * @var string $charset email encoding (iso-8859-1 default)
	 */	 	
	
	public $charset;
	
	/**
	 * @var string $mime Email mime type
	 */
	
	public $mime;
	
	/**
	 * $var bool $debug enable debug mode to display errors etc.
	 */	 	
	
	public $debug;
	
	/**
	 * @var string $body email body
	 */	 	
	
	protected $body;
	
	/**
	 * @var string $header email header
	 */	 
	
	protected $header;
	
	/**
	 * @var array $attachments each attachment is stored in this array
	 */	 	
	
	protected $attachments = Array();

	/**
	 * @var array $priorities priority with description
	 */	 		
	protected $priorities = Array(1 =>
		'1 (Highest)',
		'2 (High)',
		'3 (Normal)',
		'4 (Low)',
		'5 (Lowest)');
	
	/**
	 * constructor
	 */
	 
	public function __construct()
	{
		$this->clear();
	}

	/**
	 * Set default attributes
	 */
	 
	public function clear()
	{
		$this->mime        = "text/plain";
		$this->message     = "";
		$this->charset     = "utf-8";
		$this->from        = "";
		$this->fromName    = "";
		$this->to          = "";
		$this->cc          = "";
		$this->bcc         = "";
		$this->subject     = "";
		$this->returnPath  = "";
		$this->notify      = "";
		$this->priority    = 0;
		$this->debug       = FALSE;
		
		$this->clearAttachments();
	}

	/**
	 * Check if given email is valid
	 *
	 * @param string $email  email to be checked
	 * @return bool   true if email is valid	 
	 */
	 
	public static function isValidEmail($email)
	{
		return eregi("^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$", $email) != 0;
	}
	
	/**
	 * return mime type for given file
	 *
	 * @param string $file file name
	 * @return string mime-type for given file 
	 */
	 
	public static function getMimeType($file)
	{
		// mime types for files
		static $mimeTypes = Array(
			'.gif'  => 'image/gif',
			'.jpg'  => 'image/jpeg',
			'.jpeg' => 'image/jpeg',
			'.jpe'  => 'image/jpeg',
			'.bmp'  => 'image/bmp',
			'.png'  => 'image/png',
			'.tif'  => 'image/tiff',
			'.tiff' => 'image/tiff',
			'.swf'  => 'application/x-shockwave-flash',
			'.doc'  => 'application/msword',
			'.xls'  => 'application/vnd.ms-excel',
			'.ppt'  => 'application/vnd.ms-powerpoint',
			'.pdf'  => 'application/pdf',
			'.ps'   => 'application/postscript',
			'.eps'  => 'application/postscript',
			'.rtf'  => 'application/rtf',
			'.bz2'  => 'application/x-bzip2',
			'.gz'   => 'application/x-gzip',
			'.tgz'  => 'application/x-gzip',
			'.tar'  => 'application/x-tar',
			'.zip'  => 'application/zip',
			'.rar'  => 'application/rar',
			'.js'   => 'text/javascript',
			'.html' => 'text/html',
			'.htm'  => 'text/html',
			'.txt'  => 'text/plain',
			'.css'  => 'text/css'
		);

		$att = StrRChr(StrToLower($file), ".");
		if(!IsSet($mimeTypes[$att]))
			return "application/octet-stream";
		else
			return $mimeTypes[$att];
	}

	/**
	 * delete all attachments for this email
	 */
	 
	public function clearAttachments()
	{
		$this->attachments = Array();
	}

	/**
	 * add attachment encoded by base64
	 *
	 * @param string $filename     path to file on server
	 * @param string $inner_name   file name displayed in email else filename
	 * @param string $mime         own mime type if specified else get it from inner name
	 */
	 
	public function addAttachment($filename, $inner_name = "", $mime = "")
	{
		if(!file_exists($filename))
		{
			throw new EmailException("Attachment $filename file not found");
		}

		if(!is_readable($filename))
		{
			throw new EmailException("Unable to read $filename attachment");
		}

		if($inner_name == "")
		{
			$inner_name = basename($filename);
		}

		// if not mime selected - look up into MimeTypes array		
		if($mime == "")
		{
			$mime = $this->getMimeType($inner_name);
		}
			
		$attachment = "";
		
		$attachment .= "--".BOUNDARY."\n";
		$attachment .= "Content-Type: $mime; name=\"".$inner_name."\"\n";
		$attachment .= "Content-Transfer-Encoding: base64\n";
		$attachment .= "Content-Disposition: inline; filename=\"".$inner_name."\"\n\n";
		$attachment .= chunk_split(base64_encode(file_get_contents($filename)));
		
		array_push($this->attachments, $attachment);
		
	}

	/**
	 * Strip NL
	 *
	 * @param string $string to be stripped
	 * @return string $string without white characters	 
	 */

	protected function stripnl($string)
	{
		return str_replace(array("\n", "\r", "\t"), "", $string);
	}

	/**
	 * Send email
	 *
	 * @param string $emailfile   if specified the current email will be also sent as attachment
	 */
	 
	public function send($emailfile = "")
	{
		$this->body        = "";
		$this->header      = "";

		if(strlen($this->from))
			if(!$this->isValidEmail($this->from))
				throw new EmailException("From: ".$this->from." is not valid email");

		if(strlen($this->returnPath))
		{
			if(!$this->isValidEmail($this->returnPath))
				throw new EmailException("Return Path ".$this->returnPath." is not valid email");
			$this->header .= "Return-path: <".$this->returnPath.">\n";
		}
		
		if(strlen($this->from))
			$this->header .= "From: ".$this->stripnl($this->fromName)." <".$this->from.">\n";

		$invalidEmail = $this->getInvalidEmail($this->to);
		if(!Empty($invalidEmail))
			throw new EmailException("Email To: $invalidEmail is not valid!");
		
		if(!Empty($this->cc))
		{
			$invalidEmail = $this->getInvalidEmail($this->to);
			if(!Empty($invalidEmail))
				throw new EmailException("Email Cc: $invalidEmail is not valid!");
			$this->header .= "Cc: ";
			$this->header .= is_array($this->cc) ? implode(", ", $this->cc) : $this->cc;
			$this->header .= "\n";
		}
			
		if(!Empty($this->bcc))
		{
			$invalidEmail = $this->getInvalidEmail($this->to);
			if(!Empty($invalidEmail))
				throw new EmailException("Email Bcc: $invalidEmail is not valid!");

			$this->header .= "Bcc: ";
			$this->header .= is_array($this->bcc) ? implode(", ", $this->bcc) : $this->bcc;
			$this->header .= "\n";
		}

		$this->header .= "Mime-Version: 1.0\n";
		
		if(IntVal($this->notify) == 1)
			$this->header .= "Disposition-Notification-To: <".$this->from.">\n";
		else if(strlen($this->notify))
			$this->header .= "Disposition-Notification-To: <".$this->notify.">\n";
			
		if(!Empty($this->attachments))
		{
			// header with attachments
			$this->header .= "Content-Type: multipart/mixed; boundary=\"".BOUNDARY."\"\n";
			$this->header .= "Content-Transfer-Encoding: 7bit\n";
			$this->body   .= "This is a multi-part message in MIME format.\n\n";
		}
		else
		{
			// header with no attachments
			$this->header .= "Content-Transfer-Encoding: 8bit\n";
			$this->header .= "Content-Type: ".$this->stripnl($this->mime)."; charset=\"".$this->stripnl($this->charset)."\"".(Empty($emailfile) ? "" : "; name=\"$emailfile\"")."\n";
			$this->body   .= $this->message;
		}

		if($this->priority)
			$this->header .= "X-Priority: ".$this->priorities[$this->priority]."\n";
		
		if(!Empty($this->attachments))
		{
			$this->body .= "\n\n--".BOUNDARY."\n";
			$this->body .= "Content-Transfer-Encoding: 8bit\n";
			$this->body .= "Content-Type: ".$this->stripnl($this->mime)."; charset=\"".$this->stripnl($this->charset)."\"".(Empty($emailfile) ? "" : " name=\"$emailfile\"")."\n";
			$this->body .= "Mime-Version: 1.0\n\n";
			$this->body .= $this->message."\n\n\n";

			foreach($this->attachments as $attachment)
			{
    			$this->body .= $attachment;
			}
			
			// end of email
			$this->body .= "--".BOUNDARY."--";
		}
		
		// debugging
		if($this->debug)
		{
			echo "<pre>";
			echo "\nTO\n".HTMLSpecialChars($this->to);
			echo "\nSUBJECT\n".HTMLSpecialChars($this->subject);
			echo "\nBODY\n".HTMLSpecialChars($this->body);
			echo "\nHEADER\n".HTMLSpecialChars($this->header);
			echo "</pre>";
		}

		// send to more people, if param is array of emails
		if(is_array($this->to))
		{
			foreach($this->to as $email_to)
			{
				$this->sendTo($email_to);
			}
		}
		else
		{
			$this->sendTo($this->to);
		}
	}
	
	/**
	 * Send email to 1 email only
	 * 
	 * @param string $to target email	 	 
	 */	 	
	
	protected function sendTo($to)
	{
		if(!@mail($to, "=?UTF-8?B?".base64_encode($this->subject)."?=", $this->body, $this->header))
		{
			throw new EmailException("PHP::Mail() failed sent email to $to");
		}
	}
	
	/**
	 * Return invalid email if some else null
	 *
	 * @param mixed $email  email of array of emails to be checked
	 * @return string 	 invalid email if any
	 */
	 
	protected function getInvalidEmail($email)
	{
		if(is_array($email))
		{
			foreach($email as $my_email)
				if(!$this->isValidEmail($my_email))
					return $val;
		}
		else
		{
			if(!$this->isValidEmail($email))
				return $email;
		}
		
		return NULL;
	}
}

?>



เวลาเรียกใช้งานก็

Code (PHP)
include "mail.php";

$Mail = new CMail;
	
		$Mail->from     = $email_txt; // อีเมล์ผู้ส่ง
		$Mail->fromName = $name_txt; // ชื่อผู้ส่ง
		$Mail->to       = Array("[email protected]","[email protected]"); // ใส่อีเมล์ที่จะส่งไป
		$Mail->subject  = $keep_subject; // หัวข้อ
		
                $Mail->message  = $keepfile; // รายละเอียดที่จะส่ง สามารถส่งเป็นรูปแบบ html ได้
		$Mail->charset  = "utf-8";
		$Mail->mime     = "text/html";
		
		
		$rand_time = time(); 
		
		$name_file  = "a".$rand_time.rand(1111,9999);
		if($_FILES["file1"]["name"]!= "") //[1]
		{  
		  $type_file =  get_typefile($_FILES["file1"]["name"]); // ฟังก์ชันนี้คุณเขียนเองนะ คืออ่านชนิดไฟล์ที่อัพโหลดมา .jpg หรืออะไรก็แล้วแต่
		  $rename = "upload/email/file1/".$name_file.".".$type_file;
		  if(move_uploaded_file($_FILES["file1"]["tmp_name"],$rename))
		  {
			 $file1 = $name_file.".".$type_file;
			 $Mail->AddAttachment("upload/email/file1/".$file1);
			 @unlink("upload/email/file1/".$file1);
		  }
		} //[1]
		
		
		$Mail->Send();




ประวัติการแก้ไข
2014-02-17 16:39:56
2014-02-17 16:41:00
2014-02-17 16:43:38
2014-02-17 16:45:25
2014-02-17 16:48:26
2014-02-17 23:11:14
2014-02-17 23:12:43
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-17 16:34:21 By : bank1324
 


 

No. 4



โพสกระทู้ ( 4 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณครับ งงมากๆ ผมไม่เก่งเลย เอามารวมกับของผมยิ่งงง อยากให้แก้โค้ตให้ผมเลยได้ไหมครับ ผมทำไม่เป็นแล้วจริงๆ ทำมาหลายวันแล้ว ท้อเลย


ประวัติการแก้ไข
2014-02-17 17:26:13
2014-02-17 17:28:17
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-17 17:26:08 By : songpolzaa
 


 

No. 5



โพสกระทู้ ( 5,105 )
บทความ ( 4 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


มันไม่มีอะไรตรงตามต้องการหรอก....ถ้าไม่เร่งรีบก็ลองดู...
แต่ถ้าเร่งเอาจบโปรเจ็คขอเอาใจช่วยนะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-17 17:44:52 By : apisitp
 


 

No. 6

Guest


ทำมา2วันแล้วยังไม่ได้เลย ช่วยหน่อยนะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-18 12:55:12 By : songpolzaa
 


 

No. 7



โพสกระทู้ ( 4 )
บทความ ( 0 )



สถานะออฟไลน์


และมาจนถึงวันนี้ผมก็ยังทำไม่ได้ พี่ๆครับ ช่วยผมด้วยนะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-21 08:58:14 By : songpolzaa
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ขอความช่วยเหลือครับ PHP ส่งเมลแนบไฟล์ด้วยครับ ช่วยด้วยครับ ไม่นั้นไม่จบแน่ แก้โค้ดให้หน่อยครับติดอันเดียว
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่