Jump to content

php to send email attachments


gilk9

Recommended Posts

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 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.