views:

344

answers:

4

Hello! I am using phpmailer class to send email and i am attaching a file to the mail. The email is sent successfully but the attachment which is an sql file is empty which should not be the case. I have tried with an image file, but it seems that every file that i attach is empty. Can anyone help me solve this problem please?

$mail = new PHPMailer();
    $body = "Reminder";
    $mail->IsSMTP();  
    $mail->Host       = "mail.yourdomain.com";  
    $mail->SMTPDebug = 1;  

    $mail->SMTPAuth = true;  
    $mail->SMTPSecure = "ssl";  
    $mail->Host = "smtp.gmail.com";  
    $mail->Port = 465;  
    $mail->Username = "[email protected]";  
    $mail->Password = "abc";              
   $mail->SetFrom('[email protected]',
  'blbla');
   $mail->AddReplyTo("[email protected]","First Last");
   $mail->Subject = "Your order has been successfully placed"
   $mail->MsgHTML($body);
   $mail->AddAddress("[email protected]","xyz");
   $mail->AddAttachment("D:\b2\shop3.sql","shop3.sql");

UPDATE: I have tried to display the size of the file before attaching it, and it actually displays the file size. Can anyone help me please?

A: 

did you try:

D:\\b2\\shop3.sql

since is windows ?

LE: \\ - sorry

Mihai Iorga
@Mihai Iorga: Yes i have tried this also. But getting the same empty file.
chupinette
A: 

You could try testing the existence of the file before trying to attach it. Also, it's unknown how a \ is going to act inside of a string in PHP, so it's best practice to escape it via C standards:

$attach_filename = "D:\\b2\\shop3.sql";
if (is_file($attach_filename)) {
    $mail->AddAttachment($attach_filename,"shop3.sql");
} else {
    print "error: attachment $attach_filename doesn't exist";
}
amphetamachine
@amphetamachine: The mail is sent successfully with shop3.sql attached but it is empty.No error message is displayed.
chupinette
Yes, but your PHP coding tells PHPMailer to name the attachment "shop3.sql". That is not to say that it actually find the "D:\b2\shop2.sql" file to attach, but may instead be naming an empty/non-existent/inaccessible file's contents as "shop3.sql".Would the standard PHPMailer set display an error message if the attachment does not exist? I just had a look at the PHP code, and `$mail->AddAttachment()` should return false in the event of the file being unreadable. Maybe you should see what it returns when executed and, if false, abort the email and show the Error Message.
Lucanos
A: 

I suggest you to try swiftmailer.org instead

crrodriguez
-1 - Number one rule of answering questions: never suggest a complete rewrite unless absolutely necessary. Besides, PHPMailer works well enough.
amphetamachine
A: 

A couple of suggestions for further testing:

1 - Try and attach a simple *.txt file located in the same directory as the script being executed. This will test the Attachment side of things, and may prove whether the script is either not liking attaching any files, or just the *.sql one.

2 - If #1 works OK, move the *.txt file into the same folder which the *.sql one currently resides in, then test again. This should then prove whether the script is able to access a file via the filepath you are using.

3 - Try echoing the contents of the *.sql file, either into the web-browser output generated by the script, or into the body of the email message. This should also prove whether the script is able to read the file in question.

I may be waaaay off base with these suggestions, but they would be where I would start if I were to try and troubleshoot this one.

Lucanos