PHPMailer附件,无需物理文件即可完成

PHPMailer附件,无需物理文件即可完成,php,phpmailer,Php,Phpmailer,因此: 我以前使用xmail,当我在这里添加附件时,我传递了文件名和内容,应该在其中 像这样: // Setup mail class, recipients and body $mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip'); The AddAttachment function has four arguments: AddAttachment(PATH_TO_FILE, FILENA

因此:

我以前使用xmail,当我在这里添加附件时,我传递了文件名和内容,应该在其中

像这样:

// Setup mail class, recipients and body
$mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip');
The AddAttachment function has four arguments:

AddAttachment(PATH_TO_FILE, FILENAME, ENCODING, HEADER_TYPE)

如何使其以相同的方式工作,这样当我从PHPmailer类调用AddAttachment时,我可以传递相同的或类似的内容,这样我就不需要在服务器上发送实际的文件了?

因为AddAttachment函数需要的是路径而不是字节数据,您应该执行一个php转换为临时文件函数,然后将该路径字符串传递到您的函数中

$xmail->addAttachment('myamazingfile.pdf', $content);
乙二醇

$mail->addStringAttachment$content,$filename; 这对我很有效


完整参考:

非常好。感谢您的回答对于任何尝试此方法的人,编码可能是base64,$type是MIME类型的字符串。此时链接已断开。有一个指向文档的直接链接:该链接有效-
$prefix     = 'ConvertMediaArgs_'.time().'_';
$tempfile   = tempnam( $this->tempdir, $prefix );

// Args file create failure: kill script with TEMPFILEFAIL error
if($tempfile === false) {
    die('file could not be created');
}

// Write args as Key=Val (\n) to file
$fullpath   = $this->tempdir.$tempfile;
$content    = $someContent // <---------------- this is your file's data
$handle     = fopen( $tempfile, "w");
fwrite( $handle, $content );

// $fullpath is the path you wanna pass to your function
$xmail->addAttachment( $fullpath, $content );
AddStringAttachment($string,$filename,$encoding,$type)
$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);