ارسال فایل با ایمیل - Ghoghnus - 07-10-2011
با سلام
دوستان من میخوام با PHP ایمیل بفرستم با ارسال ایمیل مشکلی ندارم ولی میخوام 1فایل هم اتچ کنه و همراه ایمیل ارسال کنه!
ممنون میشم راهنمایی کنید
RE: ارسال فایل با ایمیل - Amin_Mansouri - 07-11-2011
کد: Class for sending mail with MIME attachments in multipart format using
external sendmail, mimencode and zip
<?
class Mail {
var $from; //The sender
var $to; //The recipient
var $subject; //The subject line
var $headers; //A hash of additional headers (headername => headervalue)
var $zipname; //The name of the file the attachments are zipped into
//($zipname == false if attachments are to be sent
//individually)
var $attachments; //An array of files to attach
var $body; //The body text of the message
//Mail constructor: initializes vars to default values. 'Nuff said.
function Mail() {
$this->from = "";
$this->to = "";
$this->subject = "";
$this->headers = array();
$this->zipname = false;
$this->attachments = array();
$this->body = "";
}
//Auxiliary method, used to guess a file's MIME type
//based on its extension. Doesn't know about too many
//extensions right now
function guessMIMEType($filename) {
//GUESS MIME TYPE
$filename = basename($filename);
if(strrchr($filename,".") == false) {
return("application/octet-stream");
}
$ext = strrchr($filename,".");
switch($ext) {
case ".gif":
return "image/gif";
break;
case ".gz":
return "application/x-gzip";
case ".htm":
case ".html":
return "text/html";
break;
case ".jpg":
return "image/jpeg";
break;
case ".tar":
return "application/x-tar";
break;
case ".txt":
return "text/plain";
break;
case ".zip":
return "application/zip";
break;
default:
return "application/octet-stream";
break;
}
}
//Cute little convenience method. Supply it with a filename to
//zip attachments to, or supply it with false if attachments are
//sent individually
function ZipAttachments($name) {
$this->zipname = $name;
}
//The workhorse method, does the actually sending of the mail.
//Doesn't check for errors so be careful!
function Send($sendmail = "sendmail") {
if($this->from == "")
$fp = popen($sendmail . " -i " . $this->to, "w");
else
$fp = popen($sendmail . " -i -f\"" . $this->from . "\"
" . $this->to, "w");
$mime_boundary = "-1747901728-1448367683-913849620=:4553";
if($fp == false)
return false;
//Write subject header
fwrite($fp,"Subject: " . $this->subject . "\n");
//Write user-defined headers
reset($this->headers);
while(list($hdrname,$hdrval) = each($this->headers)) {
fwrite($fp,$hdrname . ": " . $hdrval . "\n");
}
//If there are attachments, this needs to be a MIME message
if(count($this->attachments) > 0) {
//Write MIME headers
fwrite($fp,"MIME-Version: 1.0\n");
fwrite($fp,"Content-Type: multipart/mixed; BOUNDARY=\""
. $mime_boundary . "\"\n");
fwrite($fp,"\n");
//Write dummy message body
fwrite($fp," This message is in MIME format. The
first part should be readable text,\n");
fwrite($fp," while the remaining parts are likely
unreadable without MIME-aware tools.\n");
fwrite($fp,"\n");
//Write message text
fwrite($fp,"--" . "$mime_boundary" . "\n");
fwrite($fp,"Content-Type: text/plain; charset=US-
ASCII\n");
fwrite($fp,"\n");
fwrite($fp,$this->body);
fwrite($fp,"\n");
//Handle attachments
if($this->zipname != false) { //IF we've been told to
//zip the attachments
fwrite($fp,"--" . $mime_boundary . "\n");
fwrite($fp,"Content-Type: application/zip; name=
\"". $this->zipname . "\"\n");
fwrite($fp,"Content-Transfer-Encoding: base64
\n");
//fwrite($fp,"Content-ID: " . $content_ID . "\n");
fwrite($fp,"Content-Description:\n");
fwrite($fp,"\n");
$cmdline = "zip - ";
while(list($key, $attachment_name) = each($this->attachments))
$cmdline .= "$attachment_name ";
$cmdline .= "| mimencode -b";
$pp = popen($cmdline,"r");
while(!feof($pp)) {
$data = fread($pp,4096);
fwrite($fp,$data);
}
pclose($pp);
}
else { //no need to zip the attachments, attach them
//separately
while(list($key, $attachment_name) = each($this->attachments)) {
fwrite($fp,"--" . $mime_boundary . "\n");
fwrite($fp,"Content-Type: " . $this->guessMIMEType($attachment_name) . ";
name=\"". basename($attachment_name) . "\"\n");
fwrite($fp,"Content-Transfer-Encoding: base64\n");
//fwrite($fp,"Content-ID: " .
//$content_ID . "\n");
fwrite($fp,"Content-Description:\n");
fwrite($fp,"\n");
$pp = popen("mimencode -b $attachment_name","r");
while(!feof($pp)) {
$data = fread($pp,4096);
fwrite($fp,$data);
}
pclose($pp);
}
}
fwrite($fp,"--" . $mime_boundary . "--\n");
}
//No need for a MIME message, so it's an RFC822 message
else {
fwrite($fp,"\n");
fwrite($fp,$this->body);
}
pclose($fp);
}
}
?>
RE: ارسال فایل با ایمیل - Ghoghnus - 07-13-2011
میشه یکم توضیح بدین؟من نمی فهمم چیزی ازش
RE: ارسال فایل با ایمیل - Amin_Mansouri - 07-14-2011
کجاشو نمیتونی درک کنی ؟
اخه خودش کلی طول میکشه تا بگم
هر جاشو درک نمیکنی بگو تا بگم چیه
RE: ارسال فایل با ایمیل - Ghoghnus - 07-14-2011
من این کد را اجرا کردم هیچی نمایش نداد باید از یه فرم دیگه اطلاعات را سند کنم به این صفحه؟اگه اره میشه کد اونا بدین؟
RE: ارسال فایل با ایمیل - Amin_Mansouri - 07-14-2011
این یه کلاسه دیگه
از این یکی استفاده کن دیگه نمیخواد کد نویسی کنی :-)
http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment
RE: ارسال فایل با ایمیل - Ghoghnus - 07-14-2011
میشه قسمت اتچ را بیشتر توضیح بدین
کد: <?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
این قسمت را باید چه کنم چزوری مقدار خودم را بهش بد! ادرس فایل را چطوری بدم بهش
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
RE: ارسال فایل با ایمیل - Amin_Mansouri - 07-15-2011
اینجوری میشه :
کد: <?php
$to = "willem@geekology.co.za";
$subject = "A test email";
$random_hash = md5(date('r', time()));
$headers = "From: noreply@geekology.co.za\r\nReply-To: noreply@geekology.co.za";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("geekology.zip")));
$output = "
--PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
--PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello World!
This is the simple text version of the email message.
--PHP-alt-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is the <b>HTML</b> version of the email message.</p>
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/zip; name=geekology.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
echo @mail($to, $subject, $output, $headers);
?>
راهنمای بیشتر :
http://www.geekology.co.za/blog/2009/06/sending-emails-with-attachments-using-php-mail-function/
|