Lewis
Lewis

Reputation: 17

Attach a file to email via php

How can i attach my backup.zip file to an email. This is my current code:

<?php
unlink('./backups/backup.zip');
Zip('/home/example/public_html/', './backups/backup.zip'); // I have removed the ZIP function just to make this code look cleaner

    mail('[email protected]', '"'.date('d-m-Y h:m:s').'" Daily Backup: example.co.uk', 'You will find today's backup attached to this email. - This backup does NOT contain backups for MySQL databases. You must backup these up manually.');

?>

Thanks!

I need to attach the backup.zip file from the /public_html/backups/ folder

Basically, I am trying to create an automated daily cron that will send an email to the user with his backup attached to his email.

Upvotes: 0

Views: 142

Answers (1)

Tom
Tom

Reputation: 433

I wouldn't use PHP for this sort of stuff but a language like Perl or Python. Both have a lot of modules that handle file-sending like MIME::Lite and will work faster than PHP.

Example of a template I use for my back-ups, you could use other modules like the Tar module for handling the back-up

$msg = MIME::Lite->new(
  From    => '[email protected]',
  To      => '[email protected]',
  Subject => "Back-up completed",
  Type    => "text/plain",
  Data    => "Back-up from ".time());

$msg->attach(Type=> "application/x-tar",
             Path =>"/var/backup.tgz",
             Filename =>"backup.tgz");

$msg->send;

Upvotes: 1

Related Questions