Php Mail Attachment

john_yourspace used Ask the Experts™
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'); 



    $to = "";

    $from = "";

    $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));


    $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" .


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

    echo "

    The email was sent.


    else {

    echo "

    There was an error sending the mail.




Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if you like to use phpmailer, try here,


Thanks Logudotcom

I get

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

require_once '';

$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("","First Last");
	$mail->SetFrom("","First Last");
	$mail->AddReplyTo("","First Last");
	$address = "";
$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->AddAttachment($backupfile);      // attachment
  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       = '';
$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,

then check you can see the file inside...

see the file


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