You are here: php » php sending email with attachments
PHP: Sending email with attachments
- Written By
- PHPin24
- Submitted At
- 2009-08-13 10:53:15
- Num Views
- 576
- Category
- PHP
|
Here's code to send an HTML / TEXT email with attachments in PHP. The code automatically detects the file type and sends the supplied attached file via email. It send emails by default in HTML and a text message can also be entered. Fully customizable 1. To 2. Subject 3. HTML 4. File Attachment 5. From Email address 6. From display name 7. Reply To Email Name 8. BCC 9. Text version Please note that the function mime_content_type is depricated. We will post a new function as soon as the current PHP release does not support that function anymore. And now the function: * Sending an email with an attachments * * @param string $to - The receipient's email address * @param string $subject - The subject of the email message * @param string $html - A string containing the HTML message you want to send * @param string $attachmentFilename - The full path and name of the file to be attached * @param string $fromEmail - The email address you want the email to come from * @param string $fromEmailName - The Name to be displayed for the sender * @param string $replyToEmail - [Optional] When the user clicks reply the email will go to this address * @param string $bcc - [Optional] To BCC someone in on the mail * @param string $text - [Optional] If you want to send a text version of the email aswell supply it here */ $HTML = 'THIS IS MY MESSAGE'; $text = strip_tags($HTML); $fromEmailName = 'My Company'; $fromEmail = 'me@mycompany.com'; $to = 'test@test.com'; $bcc = 'me@mycompany.com'; $subject = 'This is the subject'; $tmpFilename = '/tmp/dfdgdf123'; $attachFilename = 'spring_chicken.jpg'; $attachType = mime_content_type($tmpFilename); $random_hash = md5(date('r', time())); $headers = "From: ".$fromEmail."\r\nReply-To: ".$fromEmail; //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($tmpFilename))); //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 <?php echo $text; ?> --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <?php echo $HTML; ?> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: <?php echo $attachType;?>; name="<?php echo basename($attachFilename);?>" 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 ); ?> DO NOT PUT IN A FUNCTION AS IT WILL NOT WORK FROM WITHIN A FUNCTION By PHPin24 @ 2009-08-13 10:53:15
|
