A
A
Alexander Ivanov2019-07-31 10:33:18
PHP
Alexander Ivanov, 2019-07-31 10:33:18

Why is the file not being sent by php?

$msg = "message";
    $file = 'https://site.ru/assets/templates/images/4.png';
    $file_size = filesize($file);
    $handle = fopen($file, "r");

    $content_attachment = fread($handle, $file_size);
    $type = mime_content_type($file);
    
    fclose($handle);
    
    $content_attachment = chunk_split(base64_encode($content_attachment));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n"; // multipart/mixed
    // $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $headers .= "From: ".$_SERVER['HTTP_HOST']." <[email protected]".$_SERVER['HTTP_HOST'].">\r\n";
    
    $content = "This is a multi-part message in MIME format.\r\n\r\n";
    $content .= "--".$uid."\r\n";
    $content .= "Content-Type:text/html; charset=utf-8\r\n";
    $content .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $content .= $msg."\r\n\r\n";
    $content .= "--".$uid."\r\n";
    $content .= "Content-Type: $type; name=\"".$filename."\"\r\n"; 
    $content .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $content .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $content .= $content_attachment."\r\n\r\n";
    $content .= "--".$uid."--";
    
    mail($emails,"$title $site_name {$_POST['forma']}", $content, $headers);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gennady S, 2019-07-31
@cimonlebedev

There can be several reasons, from mail(...) not working due to hosting settings , to an error in the headers or in the body of the request, while you do not give an error, and there is no way to reproduce your experience as a whole. The easiest way to avoid the error, and this is the right way, is to use one of the libraries, both automating the construction of the request body / headers, and allowing you to work with SMTP directly. Have a look at packagist.org for example packages nette/mail , zendframework/zend-mail , swiftmailer/swiftmailer . If you have not worked with composer, you should definitely deal with it: getcomposer.org

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question