Link to home
Start Free TrialLog in
Avatar of john_yourspace
john_yourspace

asked on

Php Mail Attachment

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

Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

if you like to use phpmailer, try here, http://phpmailer.worxware.com/index.php?pg=tutorial
Avatar of john_yourspace
john_yourspace

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
worked perfect thanks