简体   繁体   中英

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('example@gmail.com', '"'.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.

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    => 'backup@domain.com',
  To      => 'example@gmaiL.com',
  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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM