Send PHP HTML mail with attachments

前端 未结 4 2048
清歌不尽
清歌不尽 2020-11-30 02:41

I got a problem: Until today, I sent HTML mails with PHP using a header which contains

Content-type: text/html;

Now, I added functionality

4条回答
  •  情深已故
    2020-11-30 03:15

    SWIFTMAIL in php works gr8 for attachment with mails.

    Download swiftmailer from here http://swiftmailer.org/

    Look at the simple code below

    INCLUDE FILE

    require_once('path/to/swiftMailer/lib/swift_required.php');
    

    CREATE TRANSPORT

    //FOR SMTP
    // Create the Transport
    $transport = Swift_SmtpTransport::newInstance('smtp.googlemail.com', 465, 'ssl')
        ->setUsername('user@gmail.com')
        ->setPassword('gmailpassword');
    

    OR

    //FOR NORMAL MAIL
    $transport = Swift_MailTransport::newInstance();
    

    MAILER OBJECT

    // Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    

    CREATE MESSAGE OBJECT

    $message = Swift_Message::newInstance($subject)
        ->setFrom(array($from => $from))
        ->setTo($to)
        ->setBody($body);
    $message->attach(Swift_Attachment::fromPath($filepath));
    

    SEND MESSAGE

    $result = $mailer->send($message);
    

提交回复
热议问题