M
M
Mikhail Sergeev2019-11-20 12:45:01
Email
Mikhail Sergeev, 2019-11-20 12:45:01

How to correctly set up sending an email with an attachment from the site?

I'm sending a message through my form.

$to = wpcf_option_2('wpcf_email_2');
    $subject   = 'Письмо с сайта belaurum.by (юр. лицо)';

    $name_2  = esc_attr($_POST['wpcf_name_2']);
    $email_2 = esc_attr($_POST['wpcf_email_2']);

    if(empty($email_2)) {
      $email_2 = '[email protected]';
    }
    $address_2 = esc_attr($_POST['wpcf_address_2']);
    $msg_2_2 = esc_attr($_POST['wpcf_msg_2']);
    $ur_name_2 = esc_attr($_POST['wpcf_ur_name_2']);

    $msg_2 = '<b>E-mail адрес:</b> ';
    $msg_2 .= $email_2;
    $msg_2 .= '<br />';

    $msg_2 .= '<b>Aдрес нахождения юридического лица:</b> ';
    $msg_2 .= $address_2;
    $msg_2 .= '<br />';
    $msg_2 .= '<b>Наименование юридического лица:</b> ';
    $msg_2 .= $ur_name_2;
    $msg_2 .= '<br />';
    $msg_2 .= '<b>Текст сообщения:</b>';
    $msg_2 .= $msg_2_2;
    $msg_2 .= '<br />';

    if(!empty($_FILES['wpcf_file_2']['name'])) {
      // $filename = $_FILES['wpcf_file_2']['name'];
      $message = "Имя пользователя: ".$name_2."\nEmail пользователя ".$email_2."\n\nАдрес сайта: ".$_SERVER['HTTP_REFERER'];

      $boundary = md5(date('r', time()));
      $filesize = '';
      $headers = "MIME-Version: 1.0\r\n";
      $headers .= "From: " . $email_2 . "\r\n";
      $headers .= "Reply-To: " . $email_2 . "\r\n";
      $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
      $message="
      Content-Type: multipart/mixed; boundary=\"$boundary\"

      --$boundary
      Content-Type: text/plain; charset=\"utf-8\"
      Content-Transfer-Encoding: 7bit

      $message";
           if(is_uploaded_file($_FILES['wpcf_file_2']['tmp_name'])) {
               $attachment = chunk_split(base64_encode(file_get_contents($_FILES['wpcf_file_2']['tmp_name'])));
               $filename = $_FILES['wpcf_file_2']['name'];
               $filetype = $_FILES['wpcf_file_2']['type'];
               $filesize = $_FILES['wpcf_file_2']['size'];
               $message.="

      --$boundary
      Content-Type: \"$filetype\"; name=\"$filename\"
      Content-Transfer-Encoding: base64
      Content-Disposition: attachment; filename=\"$filename\"

      $attachment";
           }
         $message.="
      --$boundary--";


    } else {

      $headers = array(
       "MIME-Version: 1.0",
       "From: $name_2 <$email_2>",
       "Content-Type: text/html; charset=\"" . get_option('blog_charset') . "\"",
     );
     $message = '';
     $message .= "<b>$name_2</b> написал:<br /><br />";
 		$message .= str_replace("\n", "<br />", $msg_2);
    }



    if( wp_mail( $to, $subject, $message, $headers) ){
      echo '<div class="succsess"><p>Ваше письмо было отправлено. Наш менеджер свяжется с Вами в ближайшее время</p></div>';

the letter is sent, but on email it comes like this
5dd50dff53104808234095.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question