email a document using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xion.truth
Forum Newbie
Posts: 4
Joined: Wed Jul 01, 2009 3:33 pm

email a document using php

Post by xion.truth »

if anyone can please help the following problem

I have the following code and it will create a text file and email it to the desired recipient.

however i can not send text body.

There are two fields $email_txt and $email_message i am trying to get $email_txt into the $email_message which currently is sending the file

Please help

Code: Select all

 
if($_POST['emailThis'] == '1')
{
//get the grpid for this item
$check = mysql_query("SELECT *
                      FROM groups 
                      WHERE GRP_phone = '$_POST[phone]' and GRP_email = '$_POST[email]'
                      limit 1")or die(mysql_error());
$info = mysql_fetch_array( $check ); 
 
//--------------------
//create text file
$ourFileName = "../company/emailfile/sample.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
//
//edit temp file
$myFile = "../company/emailfile/sample.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "contents of my text file";
 
//
 
//email temp file
    $fileatt = "../folder/emailfile/sample.txt"; // Path to the file
    $fileatt_type = "application/octet-stream"; // File Type
    $fileatt_name = "index.cmrl"; // Filename that will be used for the file as the attachment
    
    $email_from = "Sales@company.com"; // Who the email is from
    $email_subject = "Welcome to company.com"; // The Subject of the email
   [color=#FF0000] $email_txt = "This email contains the index.cmrl file that you will need to put in the root directory of your web server."; // Message that the email has in it
    [/color]
    $email_to = "$_POST[email]"; // Who the email is too
    
    $headers = "From: ".$email_from;
    
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    
[color=#FF0000]    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message . "\n\n";[/color]
    
    $data = chunk_split(base64_encode($data));
    
  [color=#FF0000]  $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";[/color]
    
    $ok = @mail($email_to, $email_subject, [color=#FF8000]$email_message[/color], $headers);//$email_txt is not currenty in anything but everytime i add it i get an error
    
    if($ok) {
    
    } else {
    die("Sorry but the email could not be sent. Please go back and try again!");
    } 
}
 
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: email a document using php

Post by Eric! »

How and where in $email_message are you trying to add $email_text? What error are you getting?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: email a document using php

Post by Skara »

Agreed, please post the error. If it works before but not after, it's something you're doing when you add it.

It seems you have a lot of unconnected code here that doesn't yet do anything. In the future I would suggest doing one thing at a time, making sure it works at every step. It's kinda hard to read this and figure out what's being used and what isn't.

On another note..

Code: Select all

$ourFileName = "../company/emailfile/sample.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
can be simplified as...

Code: Select all

touch("../company/emailfile/sample.txt");

Code: Select all

$myFile = "../company/emailfile/sample.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "contents of my text file";
^ you're not doing anything here (???)

Code: Select all

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
//=>
$data = file_get_contents($file);
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: email a document using php

Post by thomas777neo »

I would suggest that you use:

PHP Mailer:

http://sourceforge.net/projects/phpmailer/

Or the Zend Framework Mail Functionality:

http://framework.zend.com/manual/en/zend.mail.html

It cuts out all the dirty work, you just use the classes and fill in the parameters
Post Reply