PHP Send HTML mail

前端 未结 3 625
情话喂你
情话喂你 2021-01-26 21:46

I am trying to send an HTML mail through PHP, but I can\'t seem to get it working.

This is my code:

    // multiple recipients
    $to  = \'mail\';

             


        
相关标签:
3条回答
  • 2021-01-26 22:33

    I use SwiftMailer:

    require_once('../lib/swiftMailer/lib/swift_required.php');
    ...
    function sendEmail(){
      //Sendmail
      $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
    
      //Create the Mailer using your created Transport
      $mailer = Swift_Mailer::newInstance($transport);
    
      $body="Dear $fname,\n\nYour job application was successful. \n\nYours,\n\nEamorr\n\n\n\n\n\n\n";
    
    
      //Create a message
      $message = Swift_Message::newInstance('Subject goes here')
        ->setFrom(array($email => "no-reply@yourdomain.com"))
        ->setTo(array($email => "$fname $lname"))
        ->setBody($body);
    
      //Send the message
      $result = $mailer->send($message);
    }
    

    You can send both plaintext and html email with ease.

    0 讨论(0)
  • 2021-01-26 22:43

    I don't think you have to put the To: line in the header as it is a parameter of the mail function. However some mail clients don't like light headers, here's mine which is working:

    $header = 'From: "Contact" <mail>'.PHP_EOL.
                      'Reply-to: <mail>'.PHP_EOL.
                      'MIME-Version: 1.0'.PHP_EOL.
                      'Content-Type: text/plain; charset=utf-8'.PHP_EOL.
                      'Content-Transfer-Encoding: 8bit'.PHP_EOL.
                      'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
    
    0 讨论(0)
  • 2021-01-26 22:43

    Fixed.

    I found this which for some reason worked perfectly on my server:

    // Set The Headers:
    $headers        =   'From: "Me" <info@skillsexchangenetwork.net>'.PHP_EOL.
    'Reply-to: <Me@Him.com>'.PHP_EOL.
    'Cc: <Her @There.com>'.PHP_EOL.
    'MIME-Version: 1.0'.PHP_EOL.
    'Content-type: text/html; charset=iso-8859-1'.PHP_EOL.
    'Content-Transfer-Encoding: 8bit'.PHP_EOL.
    'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
    // Send:
    mail($to, $subject, $message, $headers);
    

    Thanks for the input guys.

    0 讨论(0)
提交回复
热议问题