24

所以:

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

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

4

3 回答 3

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

例如

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

http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#method_addStringAttachment

于 2012-06-22T21:20:30.477 回答
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 );
于 2012-06-22T21:11:51.997 回答
0
$mail->addStringAttachment($content, $filename);

对我来说效果很好。

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

于 2019-07-05T13:15:12.113 回答