如何在 php 中的 phpmailer 函数中传递动态值

How to pass dynamic value in phpmailer function in php

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <noreply@gmail.com>' . "\r\n";
$headers .= 'Cc: admin@gmail.com' . "\r\n";

$email = new PHPMailer();
$email->isHTML(true);
$email->From      = '<noreply@gmail.com>';
$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();

文件没有发送,如果我在上面的部分中将变量作为参数传递,它就不能工作,而在静态值中它可以工作。

您向文件提供 url 而不是 path 作为参数。

你可能想使用类似的东西:

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

The command to attach a local file is simply $mail->addAttachment($path);, where $path contains the path to the file you want to send, and can be placed anywhere between $mail = new PHPMailer; and sending the message. Note that you cannot use a URL for the path - you may only use local filesystem path. See notes on string attachments below for how to use remote content.


你也可以使用addStringAttachment:

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

The addStringAttachment() method works just like addAttachment(), but you pass the actual contents of the item instead of a file system path. The $filename parameter is required as it's used to provide a filename for the string data at the receiver end.


Read the documentation

现在正在运行

$mail = new PHPMailer(); // defaults to using php "mail()"
        $body = $txt;

            $mail->AddReplyTo("X@gmail.com","Test Lernt");
            $mail->SetFrom('noreply@X.in', '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();