How do I specify to PHP that mail() should be sent using an external mail server?

前端 未结 6 597
盖世英雄少女心
盖世英雄少女心 2020-12-11 03:50

I have my email hosted at Rackspace Email and would like to use that as my mail server for the contact form on my website.

Looking at the php.ini file, I\'m only abl

6条回答
  •  轮回少年
    2020-12-11 04:21

    Setting up the internal mail function to use SMTP is only available on Windows. On other platforms, PHP should use the locally available sendmail or sendmail drop-in just fine.

    If you want to use a SMTP under a non-Windows server you will have to use a third party library such as my favorite Switfmailer.

    With Swiftmailer sending a email looks like this:

    require_once 'lib/swift_required.php';
    
    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
      ->setUsername('your username')
      ->setPassword('your password')
      ;
    
    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);
    
    //Create a message
    $message = Swift_Message::newInstance('Wonderful Subject')
      ->setFrom(array('john@doe.com' => 'John Doe'))
      ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
      ->setBody('Here is the message itself')
      ;
    
    //Send the message
    $result = $mailer->send($message);
    

提交回复
热议问题