سورس کد فرستادن ایمیل به همراه فایل پیوست (پی اچ پی) - Amin_Mansouri - 06-17-2012
درود
سورس زیر که با زبان php نوشته شده است یاد میگیرید که چگونه ایمیل بفرستید و همراش حتی فایل پیوست کنید.
Email with attachment
کد: 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!");
}
?>
RE: سورس کد فرستادن ایمیل به همراه فایل پیوست (پی اچ پی) - Amin_Mansouri - 06-19-2012
کد: //**************************************
// 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;
}
}
}
?>
RE: سورس کد فرستادن ایمیل به همراه فایل پیوست (پی اچ پی) - farshadm - 11-10-2014
مچکرم
|