0

I'm trying to attach my backup zip file to my gmail. about 300kb zip file iam trying to use this code

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'Exception.php';
require 'PHPMailer.php';
require 'SMTP.php';

$mail = new PHPMailer(true);

try {
    $mail->IsHTML(true);
    $mail->SetFrom('info@myDomain.net', 'ADMIN');
    $mail->Subject   = 'mysite - Backup Files - ' . date('d-M-Y');
    $mail->Body      = 'This is your backup files date: ' . date('d-M-Y');
    $mail->AddAddress( 'myMail@gmail.com' );
    $mail->addAttachment('secret-backup-03-Apr-2019-2105361.zip');

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

my zip archive is created with this php code:

<?php
function backup()
 {
  $name = "";
  $name = "./backup/backup-".date('d-M-Y').'-'.date('His').".zip";
  shell_exec("zip -q -e -9 -P 12345678 -r " . $name . " /home/user/public_html/* -x /home/user/public_html/CMD/backup/**\*");
  $secretname = "";
  $secretname = "./backup/secret-backup-".date('d-M-Y').'-'.date('His').".zip";
  shell_exec("zip -q -e -9 -P 12345678 -r " . $secretname . " " . $name);
  if (file_exists($secretname)) {
        unlink($name);
    }
 }

backup();
?>

but the mail don't arrive to my gmail i changed the file from secret-backup-03-Apr-2019-2105361.zip to testfile.rtf with the exact same code the mail arrived with the attachment!! any help ??!

EDIT: according to A4L answer i tried to send to mymail@outlook.com with the same code and the mail arrived successfully.

Now its Gmail problem. Any help??

جدو على
  • 161
  • 1
  • 8

2 Answers2

2

Make sure to use SSL to send your email and have the certificate signed for your domain. From: should have your domain, that resolves to your IP from which you are sending and also has a valid SPF record. Google found your E-Mail as a spam. If it is not in your spam folder, google just blocked it. Check your mail log (somewhere in /var/log*mail), it should have a link to google support page with instructions on how to make your mail to get delievered.

jg6
  • 318
  • 2
  • 12
  • your answer seems good. now i tried to send to mymail@outlook.com with the same code and the mail arrived successfully. i checked log folder and nothing there about gmail? – جدو على Apr 03 '19 at 22:12
  • I tried `mail-tester.com` and the result was `Wow! Perfect, you can send, score 9.9/10` valid SPF record. but `You're listed in 1 blacklist from 25 of the most common IPv4 blacklists.` – جدو على Apr 04 '19 at 14:09
1

Debug one thing at a time. Given that your message is actually arriving, it's not the sending process you need to worry about. If you generate the attachment and send it and it fails, you don't know if it's the generation or the send that's not happy, so double check that your generation works first, by itself.

If you're completely sure that your backup for is generated correctly (I note that your backup function does not return a value, so there's no way to check if it failed), check that the attachment operation works. addAttachment() returns boolean false if the attachment fails, so check that:

if (!$mail->addAttachment('secret-backup-03-Apr-2019-2105361.zip')) {
    throw new Exception('Attachment failed');
}

I would also recommend sending via SMTP rather than mail() (which you're currently using), as SMTP is faster, safer, and much easier to debug:

$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPDebug = 2;

Your backup function looks potentially unsafe: make sure you apply escapeshellarg() to all generated arguments that are passed to a shell.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • I added `throw new Exception('Attachment failed');` and still no errors. thanks for your advice about my backup function. – جدو على Apr 04 '19 at 13:38
  • `Debug one thing at a time.` OK, first I generate my backup file and move it to the same folder with mail.php and use absolute path, and nothing happen. then i tried to change the type of the file from `ZIP` to `RTF` to check if the problem is in the file type, then the message arrived with the same code. so it seems to be in the `ZIP` file. so i tried to remove encryption and password protection and try again with the same code, also the message not arrived. so I tried to send to `mymail@outlook.com` rather than `mymail@gmail.com`, and the message arrived successfully. – جدو على Apr 04 '19 at 13:45
  • SO now I think the problem is in the `GMail` . GMail block my message when it contain ZIP file but outlook don't. – جدو على Apr 04 '19 at 13:48
  • `shell_exec("zip -q -e -9 -P 12345678 -r " . escapeshellarg($secretname) . " " . escapeshellarg($name));` worked perfectly. thanks. – جدو على Apr 04 '19 at 14:02
  • It wouldn't surprise me if gmail blocks the zip attachment. That shell_exec call looks better. – Synchro Apr 04 '19 at 14:04