Send attachments with PHP Mail()?

前端 未结 14 2762
北恋
北恋 2020-11-21 04:46

I need to send a pdf with mail, is it possible?

$to = \"xxx\";
$subject = \"Subject\" ;
$message = \'Example message with html\';
$header         


        
14条回答
  •  耶瑟儿~
    2020-11-21 05:03

    Swiftmailer is another easy-to-use script that automatically protects against email injection and makes attachments a breeze. I also strongly discourage using PHP's built-in mail() function.

    To use:

    • Download Swiftmailer, and place the lib folder in your project
    • Include the main file using require_once 'lib/swift_required.php';

    Now add the code when you need to mail:

    // Create the message
    $message = Swift_Message::newInstance()
        ->setSubject('Your subject')
        ->setFrom(array('webmaster@mysite.com' => 'Web Master'))
        ->setTo(array('receiver@example.com'))
        ->setBody('Here is the message itself')
        ->attach(Swift_Attachment::fromPath('myPDF.pdf'));
    
    //send the message          
    $mailer->send($message);
    

    More information and options can be found in the Swiftmailer Docs.

提交回复
热议问题