Jump to content

Emailing An Attachment?


cproctor
 Share

Recommended Posts

HelloI am completely new to PHP and I am writing a script to email information enter from a HTML form to myself. This script appears to be working, but I was also thinking about adding some script that, based on what the user enters, emails a PDF document to them. I did not see any parameter with the mail() command to include an attachment, so I was wondering if there is another way of handling this?Thank you!Chris

Link to comment
Share on other sites

I think that these url would be very usefulhttp://www.astahost.com/info.php/create-pdf-php_t4972.htmlhttp://www.php.net/manual/en/book.pdf.phpwhat did you think about this:

<?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: 7bitHello 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"; ?>

Edited by gar onn
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...