An7hony Posted February 16, 2011 Share Posted February 16, 2011 Hi I need to attach a file to an email from my database records So far i have: function sendNewCandidate($user, $email, $agent, $jobtitle, $useremail, $cv) { $from = "From: ".$user." <".$useremail.">"; $subject = "New applicant"; $attachment = chunk_split(base64_encode(file_get_contents('http://www.website.co.uk/$cv'))); $body = $agent.",\n\n" ."".$user." has applied for your job ".$jobtitle."." ."We suggest you go to your admin panel and view the CV" ."- Thank you. "; return mail($email, $subject, $body, $from); } }; $cv holds the location of the file for the user. These files are generally .doc I have read i may need something like this: Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment but how would i incorporate this? Thanks guys Quote Link to comment Share on other sites More sharing options...
An7hony Posted February 16, 2011 Author Share Posted February 16, 2011 i have just read this, so i'm going to try it out: $my_file = "somefile.zip"; $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/"; $my_name = "Olaf Lederer"; $my_mail = "my@mail.com"; $my_replyto = "my_reply_to@mail.net"; $my_subject = "This is a mail with attachment."; $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf"; mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); Quote Link to comment Share on other sites More sharing options...
An7hony Posted February 16, 2011 Author Share Posted February 16, 2011 At the moment i am getting: Warning: filesize() [function.filesize]: stat failed for http://www.website.co.uk/elements/$cv in /home/*****/public_html/elements/mailer.php on line 93 Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/******/public_html/elements/mailer.php on line 95 function sendNewCandidate($user, $email, $agent, $jobtitle, $useremail, $cv) { $file = 'http://www.website.co.uk/elements/$cv'; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $subject = "New applicant"; $header = "From: ".$user." <".$useremail.">\r\n"; $header .= "Reply-To: ".$useremail."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $agent.",\n\n" ."".$user." has applied for your job ".$jobtitle."." ."We suggest you go to your admin panel and view the CV" ."- Website. "; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$cv."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$cv."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; return mail($email, $subject, "", $header); } }; can anyone explain? Thanks Quote Link to comment Share on other sites More sharing options...
An7hony Posted February 16, 2011 Author Share Posted February 16, 2011 ah it may be because $cv reads path/filename.doc instead of filename.doc Quote Link to comment Share on other sites More sharing options...
An7hony Posted February 16, 2011 Author Share Posted February 16, 2011 ok i am not getting any errors now. This is what i have: function sendNewCandidate($user, $email, $agent, $jobtitle, $useremail, $cv) { $file = $_SERVER['DOCUMENT_ROOT']."/elements/".$cv; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $subject = "New applicant"; $header = "From: ".$user." <".$useremail.">\r\n"; $header .= "Reply-To: ".$useremail."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $agent.",\n\n" ."".$user." has applied for your job ".$jobtitle."." ."We suggest you go to your admin panel and view the CV" ."- Web Site. "; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$name."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; return mail($email, $subject, "", $header); } }; Although instead of the file being attached to the email i am getting 0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAIgAAAAAAAAAA etc etc Quote Link to comment Share on other sites More sharing options...
An7hony Posted February 16, 2011 Author Share Posted February 16, 2011 this is similar to what i'm getting instead of the file attachment: This is the body of the message. --frontier Content-Type: application/octet-stream Content-Transfer-Encoding: base64 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg== --frontier-- http://en.wikipedia.org/wiki/MIME Quote Link to comment Share on other sites More sharing options...
An7hony Posted February 16, 2011 Author Share Posted February 16, 2011 ok i added: $content2 = file_get_contents($file); i have the attachment although when i open the file it is reading code and symbols? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 16, 2011 Share Posted February 16, 2011 Take a look at this very good page about sending email with PHP. Also take a look at the PHPMailer package. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.