带有附件的PHP mail()函数不起作用

时间:2019-05-13 05:37:44

标签: php html

带有附件(简历)的hp mail()函数不起作用,我在下面的代码中使用了它,但是显示错误提示发送邮件失败,我不明白问题出在什么地方。我也尝试了其他方法,但是它们都显示相同“邮件失败”错误,下面是我的代码,

   $config['upload_path'] = dirname($_SERVER["SCRIPT_FILENAME"]) . "/assets/backend/uploads";
        $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx';
         $this->load->library('upload',$config);
         if($this->upload->do_upload('images'))
        {
           $filedata = $this->upload->data();
            $filename = $filedata["raw_name"].$filedata["file_ext"];

        }

$to = 'testname@gmail.com'; 

$subject = 'Test email with attachment'; 

$random_hash = md5(date('r', time())); 

$headers = "From: hxxxxx6@gmail.com\r\nReply-To: hxxxxx6@gmail.com.com"; 

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

$attachment = chunk_split(base64_encode(file_get_contents(base_url().'assets/backend/uploads/'.$filename))); 

ob_start(); 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/pdf; name="base_url().'assets/backend/uploads/'.$filename"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 

$message = ob_get_clean(); 

$mail_sent = mail( $to, $subject, $message, $headers ); 
"Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

0 个答案:

没有答案