I have ok.rar file...., and i want to send it via attachment.. and i tried this script.. This sending mail with attachment but i am getting the attachment name as..., geekology.rar as i specified in $output, whats wrong with it..! <?php $to = "strkgroups@gmail.com"; $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("ok.rar"))); $output = " Content-Type: application/rar; name=geekology.rar Content-Transfer-Encoding: base64 Content-Disposition: $attachment "; echo mail($to, $subject, $output, $headers); ?> PHP: if its wrong any body suggest to send my ok.rar with mail () Thanks a lot
Hi, you could use the Zend_Mail part of the Zend_Framework, you can use the Zend_Mail part standalone (you dont have to be using the framework itself). It is very reliable for sending emails in PHP. I had many problems with mail() and the sending of attachments so I decided to use Zend_Mail as it does all of the hard work for you, you need to download the Zend_Library, then its as simple as follows to send attachments: // require Zend_Mail class require_once('library/Zend/Mail.php'); // create instance of Zend_Mail $mail = new Zend_Mail(); // set from, to and subject $mail->setFrom('info@test.com', 'Mr Tester'); $mail->addTo($to, 'Recipient'); $mail->setSubject('Email Test'); // build html content $content = "<p>Dear Recipient</p>"; $content .= "<p>This is a test email</p>"; $content .= "<p>Please delete</p>"; // set the plain text version of the email using strip_tags() $mail->setBodyText(strip_tags($content)); // add the html email content $mail->setBodyHtml($content); // add the attachment $fileContents = file_get_contents('path/to/file/filename.ext'); $attachment = $mail->createAttachment($fileContents); $attachment->filename = 'filename.ext'; // send the email $mail->send(); PHP:
Hi again, -> is object related syntax. In the above code: $mail is an object derived from the class Zend_Mail . This is done with the "new" keyword. e.g. $mail = new Zend_Mail(); makes $mail into an instance of Zend_Mail. You can think of a class as blueprint, container or package of functionality. Kinda like a box where you can store related functionality. To retrieve stuff from the box / object instance you use the -> syntax. $mail->send(); This simply calls a method called send() within the mail object. Note within a class structure "functions" are refered to as "methods" and object "variables" are refered to as object "properties". Ill show a simple class example: // a PHP class class My_Test_Class { // class property protected $testString = null; // a method to set the testString property public function setTestString($var) { $this->testString = $var; } // a method to get the testString property public function getTestString() { return $this->testString; } } // usage of My_Test_Class // create an instance of My_Test_Class $testClass = new My_Test_Class(); // set the test string $testClass->setTestString("Some test string!!!"); // echo the test string echo $testClass->getTestString(); // the output of the above would be. Some test string!!! PHP: Hope this helps. You can check out the Zend_Mail manual on the Zend website.
And friend.........., Just now i made this work..., after a bit of hard work.., this is beyond zends.., <?php $to = "strkgroups@gmail.com"; $subject = "A test email"; $path = "ok.rar"; $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("$path"))); $output = " Content-Type: application/rar; name=$path Content-Transfer-Encoding: base64 Content-Disposition: attachment "; echo mail($to, $subject, $output, $headers); ?> PHP: Its working perfect.., but it works only for attachments..., can you make it work with text and attachment Because if the $path is not set..., it showing errors.......!! Because of you i got good consent on these mail attachment, so please get me out of this finally.., Would be grateful to your help..!!!
Hi again, This is what I used before I used Zend. Maybe this will help you with your problem. Bear in mind that I started using Zend_Mail in all of my scripts because the following would occasionally send corupt emails. I would still suggest you use Zend_Mail, it can achieve way more than the following script with much cleaner code. The following will send email with attachment and HTML with the alternate plain text version. <? function sendmail($to,$subject='<no subject>',$template,$attachment='none',$from='Default <default@test.com>',$replyto='Default <default@test.com>') { // call makeplaintext to remove make the plain text version. $textcontent = makeplaintext($template); // use $template for the $htmlcontent $htmlcontent = $template; // create a random hash $random_hash = md5(date('r', time())); // from and reply to $headers = "From: ".$from."\r\nReply-To: ".$replyto; // Email content type $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; // if there is an attachment, deal with it. if($attachment!='none') { $attachmentcontent = chunk_split(base64_encode(file_get_contents($attachment['location']))); } //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 <? print $textcontent; ?> --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <? print $htmlcontent; ?> --PHP-alt-<?php echo $random_hash; ?>-- <? if ($attachment!='none') { ?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: <? print $attachment['type']; ?>; name="<? print $attachment['filename']; ?>" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachmentcontent; ?> --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 mail sent return true else false if ($mail_sent) { return true; } else { return false; } } function makeplaintext($template) { // strip tags and remove from $template $template = trim(strip_tags($template)); $template = str_replace(' ','',$template); return $template; } // USAGE // the attachment array $attachment = array('location' =>'path/filename.ext', // location on system 'filename'=>'filename.ext', // name of file once attached 'type' =>'file/type'); // the files mime type. see the following // http://www.webmaster-toolkit.com/mime-types.shtml // email to $to = 'myemail@me.com'; // html content $htmlTemplate = "<p>Hello</p><p>This is some HTML</p>"; // email subject $subject = "Email with attachment, HTML, and plain text"; // call sendmail and send the email sendmail($to,$subject,$htmlTemplate,$attachment); ?> PHP: Hope this helps!
Hello friend,, yours is good.., but Warning: file_get_contents(path/filename.ext) [function.file-get-contents]: failed to open stream: No such file or directory in /home2/strgraph/public_html/a.php on line 22 Ya ok ok., there i have to replace with my file to be attached, But my question is the file attachement must be just like optional..., But hear with out attachment we are unable to send the mail,