PHP mailer Script help

Glad to hear you are getting somewhere with it all… Now some info on folders for you…

When you are using a script such as PHP on a server in a folder, you can access any folder from that folder
BUT, you have to make sure that you start it from the root directory of the server. As an example. I place all
of my libraries such as Bootstrap, Text Editors, JS, JQuery and others in one folder called “lib” or whatever and
place it in the root folder of the server. Then, if one of my pages need to use a library in it, I called using the
root and “lib” like “/lib/boostrap/etc”. The PHP code knows that if it starts with a slash to start at the root of
the server.

So, my guess is that you don’t need to have it in the same folder, you just have to make sure you are pointing
all of the code to the correct level of folders…

Hmmmm, after re-reading this, it sounds like a messy explanation, but, hopefully you get the point…

It isn’t that the place it’s going has to be in the same place, I have an uploads directory that isn’t even on the same server.

Try doing something like,

[php]if ( file_exists( “path/to/file/” . filename) )
// do whatever with the file
else
// file not found[/php]

Thanks ErnieAlex - yes this makes sense to me

Astonecipher - the code that attaches the file is as follows so not quite sure how to implement your advice?
[php]$attachment = chunk_split(base64_encode(file_get_contents($_POST[“file_name”] )));[/php]

Dan, your code uses the filename from the posted form. Since you have already know where the file is
actually on the server. If it is in your “uploads”, you have to point to it… Where is it on the server? In
the root of the server? If so, then change your line from:
$attachment = chunk_split(base64_encode(file_get_contents($_POST[“file_name”] )));

To something like this:

$attachment = chunk_split(base64_encode(file_get_contents("/uploads/" . $_POST[“file_name”] )));

Note that you just add in the location of your uploaded files where it actually is…

Also, Astonecipher was showing you how to check if the file exists to make sure you are pointing to the
correct place. If not, it should display an error message…

Thanks ErnieAlex, I had to remove the first forward slash to get the script to work. A big thank you goes to you and Astonecipher!

On to my next php problem now :frowning:

my final code for all was/is:-
[php]<?php
if (isset($_POST[‘submit’]))
{
$file_name = $_POST[‘file_name’];
$doc_email = $_POST[‘doc_email’];
$doc_message = $_POST[‘doc_message’];
$file_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file_name);
$username = $_POST[‘username’];
$company = $_POST[‘company’];

//define the receiver of the email
$to = $_POST[‘doc_email’];
$subject = “$username from $company Sent you a file”;

//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: [email protected]\r\nReply-To: [email protected]”;

//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(“uploads/” . $_POST[“file_name”] )));

//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

Hello there,

<?php echo $username; ?> from <?php echo $company; ?> sent you a file. <?php echo $username; ?> wrote:- <?php echo $doc_message; ?>

–PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset=“iso-8859-1”
Content-Transfer-Encoding: 7bit

Hi there,

<?php echo $username; ?> from <?php echo $company; ?> sent you a file.

<?php echo $username; ?> wrote:-

<?php echo $doc_message; ?>

For further information about this email or for any questions you may have regarding XX please contact [email protected]

–PHP-alt-<?php echo $random_hash; ?>–

–PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/<?php echo $filetype; ?>; name="<?php echo $file_name; ?>"
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"; } ?>[/php]

Glad we could help. AND, thank you for posting the finished code in case someone else can use it…

Open a new post on the next puzzle as they seem to always keep popping up…

Sponsor our Newsletter | Privacy Policy | Terms of Service