gilk9 Posted January 5, 2010 Share Posted January 5, 2010 Hi, The following code is part of code that works when I plug the actual filename say, howtodoit.zip , into the two positions highlighted in red. Instead of the actual filename I want to use a variable containing that filename say, $filename. Can you show me how this is achieved. Thanks, D $attachment = chunk_split(base64_encode(file_get_contents('howtodoit.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: 7bit You have an attachment --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello </h2> <p>You have an attachment! <b>HTML</b>See Attachment</p> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/zip; name= "howtodoit.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"; } //end code for sending zipfile by attachment Quote Link to comment https://forums.phpfreaks.com/topic/187246-php-to-send-email-attachments/ Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 If you're not understanding how to do the most basics of PHP, I'd recommend you look up some tutorials and understand what that code is before you implement it. There are a variety of resources that could show you how to do so, without you having to do it yourself. As for your problem, replace 'howtodoit.zip' (including quotes) with $filename, make sure there are no quotes around the variable. Of course you have an empty variable now, so at the top somewhere in your script, you can write $filename = 'randomname.zip'; Or to call from url: $filename = $_GET['file']; or similar. Quote Link to comment https://forums.phpfreaks.com/topic/187246-php-to-send-email-attachments/#findComment-988856 Share on other sites More sharing options...
gilk9 Posted January 5, 2010 Author Share Posted January 5, 2010 Thank you for your time. I do understand the most basics of PHP. I only ask for help when the most basics dont work for me. As mentioned b4, if i plug a valid filename in manually in BOTH the RED-marked spaces the code works and the attachment is delivered and uncorrupted. BUT when I plug in a variable containing a valid filename (residing in the same directory as the code) (and according to your instructions) I get the MAIL SENT message but it isnt sent...and I get another message that it has been saved as dead.letter in a directory which godaddy does not allow access to. To ensure that a valid filename is contained in the variable I have echoed it successfully. For some reason the coding you suggest and I had already tried, does not do what we both thought it would. Thanks in anticipation David Quote Link to comment https://forums.phpfreaks.com/topic/187246-php-to-send-email-attachments/#findComment-989250 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.