Php Mail Attachment

john_yourspace
john_yourspace used Ask the Experts™
on
Hi guys I am trying to attach a .sql file to a mail using this code however it is not working looks to be all encoded when recived and not an attachment

$backupfile = 'dump\DB_Backup_'.$Database.'_'.date('Y-m-d').'.sql'; 

$handle = fopen($backupfile,'w'); 
fwrite($handle,$contents); 



echo("done");

mysql_close($connection);


    $to = "xxx@xx.com";

    $from = "xx@xx.ie";

    $subject = "Here is your attachment";

    $fileatt = $backupfile;

    $fileatttype = "application/octet-stream";

    $fileattname = $Database.'_'.date('Y-m-d').'.sql'; 

    $headers = "From: $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}\"";

    $message = "This is a multi-part message in MIME format.\n\n" .

    "-{$mime_boundary}\n" .

    "Content-Type: text/plain; charset=\"iso-8859-1\n" .

    "Content-Transfer-Encoding: 7bit\n\n" .

    $message .= "\n\n";

    $data = chunk_split(base64_encode($data));

    $message .= "–{$mime_boundary}\n" .

    "Content-Type: {$fileatttype};\n" .

    " name=\"{$fileattname}\"\n" .

    "Content-Disposition: attachment;\n" .

    " filename=\"{$fileattname}\"\n" .

    "Content-Transfer-Encoding: base64\n\n" .

    $data . "\n\n" .

    "-{$mime_boundary}-\n";

    if(mail($to, $subject, $message, $headers)) {

    echo "

    The email was sent.

    ";

    }
    else {

    echo "

    There was an error sending the mail.

    ";

    }



?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if you like to use phpmailer, try here, http://phpmailer.worxware.com/index.php?pg=tutorial

Author

Commented:
Thanks Logudotcom

I get

Fatal error: Call to undefined method phpmailer::SetFrom() in E:\Domains\epallas.eu\wwwroot\sqldump.php on line 77

require_once 'phpmailer.inc.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch



try {
  $mail->AddReplyTo("john@x.com","First Last");
	$mail->SetFrom("john@x.com","First Last");
	$mail->AddReplyTo("john@x.com","First Last");
	$address = "john@x.com";
$mail->AddAddress($address, "John Doe");
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $body = "Emai Body";
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML($body);
  $mail->AddAttachment($backupfile);      // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

Open in new window

try it like this

$mail->From       = 'john@x.com';
$mail->FromName = 'First Last';

that is how I have it set up and it works ok for me.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

did you download the source from here, http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/

then check you can see the file inside...


see the file

Author

Commented:
worked perfect thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial