php
  • email
  • attachment
  • 2017-04-23 87 views 0 likes 
    0
    $headers = "MIME-Version: 1.0" . "\r\n"; 
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
    
    // More headers 
    $headers .= 'From: <[email protected]>' . "\r\n"; 
    $headers .= 'Cc: [email protected]' . "\r\n"; 
    
    $email = new PHPMailer(); 
    $email->isHTML(true); 
    $email->From  = '<[email protected]>'; 
    $email->FromName = 'Name'; 
    $email->Subject = $Subject; //subject name 
    $email->Body  = $txt; //This is html message 
    $email->AddAddress($to); 
    $email->AddAttachment("http://www.domain.in/" , "invoice.pdf", 'base64', 'application/octet-stream'); 
    $email->Send(); 
    

    文件没有发送,如果我在上面的部分中传递变量作为参数,它在静态值时不工作。如何通过php中的phpmailer函数传递动态值

    回答

    1

    您正在提供url而不是 a path作为参数的文件。

    您可能需要使用类似:

    $email->AddAttachment("/path/to/invoice.pdf" , "invoice.pdf", 'base64', 'application/octet-stream'); 
    

    的命令附加一个本地文件只是 $mail->addAttachment($path);,其中$path包含路径到你想发送的 文件,并且可以放置在$mail = new PHPMailer;之间的任何位置并发送消息。请注意,您不能使用URL 的路径 - 你可以只使用本地文件系统path。有关如何使用远程内容,请参阅下面有关字符串附件的注释 。


    您还可以使用addStringAttachment

    $url = "http://www.domain.in/" 
    $mail->addStringAttachment(file_get_contents($url), 'myfile.pdf'); 
    

    addStringAttachment()方法的工作原理就像addAttachment(), 但你通过这个项目,而不是一个文件系统 path的实际内容。因为它是用于提供一个 用于在接收器端的字符串数据文件名$filename参数是必需的。


    Read the documentation

    +0

    正在我想这其中也。见下面的代码也被显示错误“无法访问文件:” @Pedro洛比托 –

    0

    现在

    $mail = new PHPMailer(); // defaults to using php "mail()" 
         $body = $txt; 
    
          $mail->AddReplyTo("[email protected]","Test Lernt"); 
          $mail->SetFrom('[email protected]', 'xxxx'); 
    
    
          $mail->AddAddress($to, "Giridhari Lal");  
          $mail->Subject = $subject;  
          $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
    
          $mail->MsgHTML($body); 
          //documentation for Output method here: http://www.fpdf.org/en/doc/output.htm 
          $pdfdoc = $pdf->Output($filename,'F');  
          $path = "invoice.pdf"; 
    
          $mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf'); 
          var_dump($mail); 
          $mail->send(); 
    
    +0

    @Pedro洛比托看到这个代码。仍然文件不发送除html内容 –

    相关问题