Attachment using phpmailer not opening in O365

I am using phpmailer to send xlsx file as an attachment
when opening the file in o365 its not opening
but i can open the generated file from server
so the issue is with attachment

please help

First, welcome to the site !

Next, this is a programming site. Please show us your code. We know about phpmailer, so just show us how you are setting the attachment up in code. Please use the tags to place the code it so it is posted correctly. Just the area of your code where you set the attachment in phpmailer…

Thanks a lot ErineAlex.
one more thing to mention- i am opening this excel in outlook.

This is my code

$attachment = ‘.Results.xlsx’;
$handle = fopen($attachment, “r”); // set the file handle only for reading the file
$file = fread($handle , filesize($attachment));

$body = “testing”;
$file = chunk_split(base64_encode($file));
$num = md5(time());

    //Normal headers 
    $headers  = "From:".$from_email." \n"; 
    $headers  .= "MIME-Version: 1.0\n"; 
	$headers  .= "Content-Type: multipart/mixed; "; 
	$headers  .= "boundary=".$num."\n"; 
    $headers  .= "--$num\n";  

    // This two steps to help avoid spam    
    $headers .= "Message-ID: <".$now." [email protected]".$_SERVER['SERVER_NAME'].">\n"; 
    $headers .= "X-Mailer: PHP v".phpversion()."\n";          

    // Attachment headers 
	$headers  .= "Content-Type:".filetype($attachment)." ";
    $headers  .= "name=\"" . $outFile . "\"\n"; 
    $headers  .= "Content-Transfer-Encoding: base64\n"; 
    $headers  .= "Content-Disposition: attachment; "; 
    $headers  .= "filename=\"" . $file_name . "\"\n\n"; 
    $headers  .= "".$file."\n"; 
    $headers  .= "--".$num."--"; 
    
    // With message        
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n"; 
    $headers .= "Content-Transfer-Encoding: 8bit\n"; 
  	$headers .= "".$body."\n";
    $headers .= "--".$num."\n";
  
$sentMailResult = mail($recipient_email, $subject, $body, $headers);

==============================

You are welcome! I have found that a lot of people have problems opening Excel sheets directly from Outlook emails as attachments. Recently, someone had this problem. I resolved it for them by having them download the attachment and then open it from their download folder. Doing it that way, it worked 100% correctly for them. I suggest you test that first before we continue. But, try this…

First, you do not need to send all the headers out for attachments. You just need to add them in one line. All of your base65 encoding is not needed, neither is the filetype or content-dispoition’s. Just attach it.
You do NOT need to read it in and condition it in any manner at all. Just load it directly. Your code is set up to send using PHP’s mail system, NOT phpmailer… Here is how you would create an email with an attachment using phpMailer system…

$attachment_file_name = "Results.xlsx";
require("phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true; 
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "hidden"; // SMTP password
$mail->addAttachment("your-folder-name/".$attachment_file_name);
$mail->From = $email;
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587; //SMTP port
$mail->addAddress("[email protected]", "your name");
$mail->Subject = "You have an email from a website visitor!";
$mail->Body ="
Name: $name<br>
Email: $email<br>
Telephone: $phone<br><br><br>
Comments: $message";
$mail->AltBody = $message;
if(!$mail->Send()) {
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

This example uses SMTP instead of POP. Either is simple! And, a host of Gmail… Hope this helps!

Thank you for quick reply.

Actually my main code is with phpmailer when it was not working then i tried with the above code

  • I tried that save option without opening directly, but still it has same issue

  • can you please give me the link to download phpmailer where i will get ```
    PHPMailerAutoload.php

this is the error while opening the excel

![image|690x103](upload://uEgyBlLAe73Sy68qAh6M2GoCDZy.png)

GO here: https://github.com/PHPMailer/PHPMailer And, select the small green button and download the ZIP version. Then, unzip it and move to your server.

I have had some issues with pointing it to the “Autoload” file. In some versions you need to set a workspace first to get it to work. The newer version needs that, I think. But, on that page, you can look down a bit and see an example how to make it work. And, SMTP and POP options are important to decide which you need to use. That depends on the server you are using. Hope this helps…

Hi Erinie,

For me it worked automatically, it was firewall issue. so after some changes of firewall it worked again

thanks a lot for your all help, i never thought i will get reply.

Very happy you figured it out. Enjoy your day…

Sponsor our Newsletter | Privacy Policy | Terms of Service