通过php将文件附加到电子邮件

时间:2013-01-26 22:25:37

标签: php sendmail

如何将backup.zip文件附加到电子邮件中。这是我目前的代码:

<?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.');

?>

谢谢!

我需要附加/ public_html / backups /文件夹

中的backup.zip文件

基本上,我正在尝试创建一个自动日常cron,它将向用户发送一封电子邮件,并将其备份附加到他的电子邮件中。

1 个答案:

答案 0 :(得分:1)

我不会将PHP用于这种东西,而是像Perl或Python这样的语言。两者都有很多处理文件发送的模块,如MIME::Lite,并且比PHP更快。

我用于备份的模板示例,您可以使用Tar module等其他模块来处理备份

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