send email with attachment using php
send email with attachment using php
<?php function mail_attachment($filename,$path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { $body = ''; $headers = ''; $file = $path.$filename; $separator = md5(time()); $attachment = chunk_split(base64_encode(file_get_contents($file))); $headers = "From: ".$from_name."<".$from_mail.">" . PHP_EOL; $headers .= "Reply-To: ".$replyto . PHP_EOL; $headers .= "MIME-Version: 1.0".PHP_EOL; $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL . PHP_EOL; $body .= "Content-Transfer-Encoding: 7bit" . PHP_EOL; $body .= "This is a MIME encoded message." . PHP_EOL; $body .= "--" . $separator . PHP_EOL; $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL; $body .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL; $body .= $message . PHP_EOL; $body .= "--" . $separator . PHP_EOL; $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . PHP_EOL; $body .= "Content-Transfer-Encoding: base64" . PHP_EOL; $body .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL; $body .= $attachment . PHP_EOL; $body .= "--" . $separator . "--"; if (mail($mailto, $subject, $body, $headers)) { echo "Message sent!"; } else { echo "ERROR sending message."; } } $my_file = "Sample_Trade_Journal.pdf"; $my_path = $_SERVER['DOCUMENT_ROOT']."/images/Journal/"; $my_name = "Your Name (or) Your Business"; $my_mail = "[email protected]"; $my_replyto = "[email protected]"; $to_email = "[email protected]"; $my_subject = "Your file has arrived!"; $requester = $_POST['Field101']; $message = "Hey $requester, Your custom email message goes here"; mail_attachment($my_file, $my_path, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message); ?>
View On WordPress










