2016-06-14 27 views
0

スリムなPHPフレームワークを使用していて、Gmail経由で電子メールを送信しようとしています。 問題は、電子メールを送信しているときに、「Slim Application Error」が発生することです。ここでSwiftMailerを使用してGmail経由で電子メールを送信

$mailer->send($message); 

は、あなたがそれを使用する前に

function send_email($email) 
{ 
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls') 
     ->setUsername('[email protected]') 
     ->setPassword('yourpassword'); 

    $mailer = Swift_Mailer::newInstance($transport); 


    // Create a message 
    $message = Swift_Message::newInstance('Test mail') 
     ->setFrom(array('[email protected]' => 'SENDER')) 
     ->setTo(array('[email protected]')) 
     ->setBody("Hello, test send mail"); 

    // Send the message 
    $mailer->send($message); 

} 
+1

どのようなエラーが発生しますか? – geggleto

+0

Swiftmailerでメールを設定して送信する方法を示すチュートリアル。 http://sgeek.org/send-email-attachment-using-swiftmailer-symfony/ –

答えて

0

電子メールを送信するために私の関数である、あなたはそれを行うには、Gmailアカウントを準備する必要があります:https://support.google.com/accounts/answer/2461835?hl=en は初期化: //交通を作成します。あなたは、あなたのconfig.phpファイルの中で、routes.php(良い習慣ではない)の中で直接使うことができます。 $輸送= Swift_SmtpTransport ::のnewInstance() - > setHost( 'smtp.gmail.com') - > setPort(465) - > setEncryption( 'SSL') - > setUsername('[email protected] ') - > setPassword(' passw ') ;

 // Create Mailer instance with our Transport. To be ready to call it. 

$mailer = Swift_Mailer::newInstance($transport); 
</code> 
Using: (Where you need to use it. eg: in UserController.php , you can use it. 
<code> 

    $message = Swift_Message::newInstance() 
        ->setSubject('Email from contact form') 
        ->setFrom(array('[email protected]'=> 'Me')) 
        ->setTo(array('[email protected]' => 'You')) 
        ->setBody("I am sending you an email from contact form"); 

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


    // Print the results, 1 = message sent! 
    print($result); 
</code> 
+1

このコードにも説明を加えてください。 –

関連する問題