1

PHP とローカル メール サーバー hMailServer で SwiftMailer パッケージを使用します。PHP コード:

<?php

require 'vendor/autoload.php';

$message=Swift_Message::newInstance();
$message->setFrom('webmaster@mail.com');
$message->setTo(array('testmail@mail.com'=>'Ilqar Rasulov'));
$message->setSubject('Delicious New Recipe');
//$message->setBody(<<<_TEXT_
//Dear James,
//You should try this: puree 1 pound of chicken with two pounds
//of asparagus in the blender, then drop small balls of the mixture
//into a deep fryer. Yummy!
//Love,
//Julia
//_TEXT_
//);
$message->addPart(<<<_TEXT_
<p>Dear James,</p>
<p>You should try this:</p>
<ul>
<li>puree 1 pound of chicken with two pounds
of asparagus in the blender</li>
<li>then drop small balls of the mixture into a deep fryer.</li>
</ul>    
<p><em>Yummy!</em></p>
<p>Love,</p>
<p>Julia</p>
_TEXT_
, "text/html");
try{
$transport= Swift_SmtpTransport::newInstance('localhost',25);
$mailer=  Swift_Mailer::newInstance($transport);

$mailer->send($message);
}  catch (Exception $ex){
    print $ex->getMessage();
}

hMailServer を構成し、パスワードを割り当てました (管理者とアカウントのパスワード)。では、swiftmailer がジョブを実行するために必要なのはホスト名とパスワードだけで、認証を必要としないのはなぜですか? ユーザー名とパスワードを構成ファイルに保存したことを思い出せません。

4

1 に答える 1

2

hMailServer から電子メールを送信するには、次のようなトランスポート オブジェクトを作成する必要があります (ここで説明されているように):

// Create the Transport the call setUsername() and setPassword()
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('username')
  ->setPassword('password')
  ;

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

smtp.example.orgまた、usernamepasswortを hMailServer の対応する設定に置き換える必要があることに注意してください。

現在、スクリプトは hMailServer を介してメールを送信するのではなく、スクリプトが配置されているサーバーからphp mail() 関数を使用して送信します。この関数では任意のFROMアドレスを使用できますmail()が、多くのメール プロバイダーはこれをスパムとして認識します。そのため、トランスポート オブジェクトを作成することをお勧めします。

于 2016-08-27T09:16:17.743 に答える