eoghanobrien/php-simple-mail

attachment problem

Closed this issue · 11 comments

Hi
when I add attachment in localhost see attached file in my email as text only

This is a multi-part message in MIME format. --11ad93eaf86ba31d942b41449e2dcf94 Content-type:text/html; charset="utf-8" Content-Transfer-Encoding: 7bit This is a test message. --11ad93eaf86ba31d942b41449e2dcf94 Content-Type: application/octet-stream; name="test.jpg" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="test.jpg" /9j/4AAQSkZJRgABAQEAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/9sAQwABAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB/9sAQwEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEB/8AAEQg

Hi @mazandnet, can you provide a test case that reproduces the error.

I can test and send email to you . What is your mail?

Just provide the code (parameters) you're using to send the email.

Thanks, which client are you seeing the issue.

I am useing this in my localhost on windows 10
whith this code i can send attache but this code don't send html emails :

final class SMail {
      
      public $attachment_name;

      public $attachment_path;

    
      public $body;

   
      public $error;

     
      public $from;

     
      public $subject;

     
      public $to;

      
      public function send() {
            // check for valid params
            if($this->to) {
                  // set boundry
                  $boundary = md5(rand(5000, 500000000) . time());

                  // set attachment if exists
                  $attachment = null;
                  if($this->attachment_path && $this->attachment_name) {
                        // check if attachment file exists
                        if(file_exists($this->attachment_path)) {
                              $attachment = chunk_split(base64_encode(file_get_contents($this->attachment_path)));
                        } else {
                              $this->error = "Failed to send mail, attachment file \"{$this->attachment_path}\" not found";
                              return false;
                        }
                  }

                  // set headers
                  $headers = null;
                  if($this->from) {
                        $headers = "From: {$this->from}\r\nReply-To: {$this->from}";
                  }
                  if($attachment) {
                        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_{$boundary}\"";
                  }

                  // set body
                  $body = null;
                  if($attachment) {
                        $body = "This is a multi-part message in MIME format.

--_1_{$boundary}
Content-Type: multipart/alternative; boundary=\"_2_{$boundary}\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

{$this->body}

--_2_{$boundary}--
--_1_{$boundary}
Content-Type: application/octet-stream; name=\"{$this->attachment_name}\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

{$attachment}
--_1_{$boundary}--";
                  } else {
                        $body = $this->body;
                  }

                  // send mail
                  if(mail($this->to, $this->subject, $body, $headers)) {
                        return true;
                  } else {
                        $this->error = "Failed to send mail (check SMTP settings)";
                        return false;
                  }

            // fail
            } else {
                  $this->error = "Failed to send mail, invalid params";
                  return false;
            }
      }
}

Which e-mail client are you using? e.g. Outlook, Mac Mail etc

I'm not sure what the code you referenced above is from but it's not using SimpleMail so I can't help you with that.

I just tested using your exact code and it works as expected in Gmail, Mail (Mac) and Outlook 2017 (Mac)

I think you should look into how the mail() function is working in your Windows environment

I am seeing this same problem with gmail (not tested other clients yet).

$mail = new SimpleMail();
	$mail
		->setSubject('Performance Report For Date Range')
		->setFrom('noreply@xxxxxxxxxxxxx.com', 'XXXXXX - Reports')
		->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
		->addGenericHeader('X-Script', 'daily_reports.php')
		->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
		->addAttachment('tempfile','performance_report.xls')
		->setMessage('Performance Report For Date Range is attached')
		->setWrap(78);


		$mail->setTo('xxxxxxxx@gmail.com', 'XXXXX');

	$send = $mail->send();

I am on CentOS Linux release 7.4.1708 using php PHP 7.1.8

The result is that the content of $headers on line 505 is in the message as content.

I have tried replacing PHP_EOL with "\r\n" however this had no effect.

I am attaching an xlsx if that makes any difference

Ignore the above, I know what the issue is. Maybe the code should watch for this, but can't hold everyone's hand.

basically I was setting the content-type..

->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')

which was applied to the email headers before the attachments headers, so the content was being treated as 'text/html' and quite rightly!