PHPMailer附件,没有物理文件

时间:2012-06-22 21:09:49

标签: php phpmailer

所以:

// 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)

我曾经使用xmail(),当我在这里添加附件时,我传递了文件名和内容,应该在其中。

像这样:

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

如何让它以相同的方式工作,所以当我从PHPmailer类调用AddAttachment()时,我可以传递相同或类似的东西,所以我不需要在我的服务器上有一个实际的文件发送?

3 个答案:

答案 0 :(得分:36)

AddStringAttachment($string,$filename,$encoding,$type)

例如

$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);

http://phpmailer.worxware.com/index.php?pg=tutorial#3

答案 1 :(得分:1)

因为AddAttachment()函数需要一个路径而不是字节数据,所以你应该将php转换为临时文件函数,然后将该路径字符串传递给你的函数

$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 );

答案 2 :(得分:0)

$mail->addStringAttachment($content, $filename);

对我来说很好。

完整参考:http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#method_addStringAttachment