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

since that AddAttachment() function is expecting a path rather than byte data, you should do a php convert to temp file function and then pass that path string into your function

$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 に答える