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.
";
}
?>
if you like to use phpmailer, try here, http://phpmailer.worxware.com/index.php?pg=tutorial
ASKER
Thanks Logudotcom
I get
Fatal error: Call to undefined method phpmailer::SetFrom() in E:\Domains\epallas.eu\wwwr oot\sqldum p.php on line 77
I get
Fatal error: Call to undefined method phpmailer::SetFrom() in E:\Domains\epallas.eu\wwwr
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!
}
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.
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following should work for you:
http://www.texelate.co.uk/blog/send-email-attachment-with-php/
You can also take a look here:
http://webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment
http://www.texelate.co.uk/blog/send-email-attachment-with-php/
You can also take a look here:
http://webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment
ASKER
worked perfect thanks