PHPMailer attachment, doing it without a physical file

后端 未结 3 1938
青春惊慌失措
青春惊慌失措 2020-11-30 13:41

So:

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


        
相关标签:
3条回答
  • 2020-11-30 14:04
    AddStringAttachment($string,$filename,$encoding,$type)
    

    eg

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

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

    0 讨论(0)
  • 2020-11-30 14:04
    $mail->addStringAttachment($content, $filename);
    

    works very well for me.

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

    0 讨论(0)
  • 2020-11-30 14:07

    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 );
    
    0 讨论(0)
提交回复
热议问题