2011-10-27 4 views
5

私は、GmailのアカウントからCakEmailとSMTPの設定を使って電子メールを送信しようとしています。CakePHP-2.0:どのように私はGmailのアカウントからCakEmailとSMTP設定を使ってメールを送ることができますか?

誰かが何段階でプロセスに何をすべきかを伝えればいいと思います。

私は今、私は「[email protected]」から任意のメールアカウントに電子メールを送信することができますどのようにアプリ/コンフィグ/ email.php =>

<?php 
class EmailConfig { 
    public $smtp = array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'username' => '[email protected]', 
     'password' => 'secret' 
    ); 
} 

に次のように追加しましたか?

それは、CakePHP-2.0

答えて

9

あなたは、GmailなどのSSL SMTPサーバを設定することができます。これを行うには、ホストに接頭辞として 'ssl://'を置き、それに応じてポートの値を設定します。例:私は現在、アウトバウンドメールを送信するためにGmailアカウントを使用しています

<?php 
class EmailConfig { 
    public $gmail = array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'username' => '[email protected]', 
     'password' => 'secret' 
    ); 
} 

http://book.cakephp.org/2.0/en/core-utility-libraries/email.html?highlight=cakeemail#CakeEmail

2

ちょうどfromを設定します。

<?php 
$email = new CakeEmail(); 
$email->from(array('[email protected]' => 'Your Name')); 
$email->to('[email protected]'); 
$email->subject('Sent from Gmail'); 
$email->send('My message'); // or use a template etc 

はそれを行う必要があります。

senderも同様に設定できます。私は100%ではありませんが、自分のウェブサイトからGmailに "メール"を送信するときに役立つと思います。おそらくスパムとして取り上げられている電子メールを止めるためです。ドキュメントから

$email->sender('[email protected]', 'MyApp emailer');

+0

魅力的なように動作します –

0

。私はテンプレートと再利用可能な電子メールセットアップ機能を使用しています。ここに私の作業コードのコピーがあります:

// app/controllers/users_controller.php 
function sendemail($subject, $body, $to, $template) { 
    $this->Email->smtpOptions = array(
     'port'=>'465', 
     'timeout'=>'30', 
     'host' => 'ssl://smtp.gmail.com', 
     'username'=>'[email protected]', 
     'password'=>'secret', 
    ); 
    $this->Email->delivery = 'smtp'; 
    //$this->Email->delivery = 'debug'; 
    $this->Email->from = 'Username <[email protected]>'; 
    $this->Email->to  = $to; 
    $this->Email->subject = $subject; 
    $this->set('body', $body); 
    $this->set('smtp_errors', $this->Email->smtpError); 
    $this->Email->send($content, $template); 
} 

// app/controllers/users_controller.php 
// Excerpt from new user method in users controller: 
function add() { 
    // ...other stuff 
    $body['user'] = $user['User']['username']; 
    $this->sendemail('Domain.com New User Signup!', $body, '[email protected]', 'newuser'); 
    // ...other stuff 
} 

// app/views/elements/email/text/newuser.ctp 
Everyone, 
Another new user just signed up for Domain. Stats below: 
User: <?php echo $body['user'] . "\r\r"; ?> 
Just thought you'd like to know :) 
-Janet 
+0

これは古いメールコンポーネントです。 – shibly

0

Swiftmailerコンポーネントを使用してください。これは使用するのが最も簡単なコンポーネントです。

http://bakery.cakephp.org/articles/mhuggins/2008/06/11/improved-swiftmailer-component

あなたが以降のCakePHP 2.0でこれを使用しながら行う必要があるいくつかの変更があります。 CakePHP 2.0は、すべての電子メールテンプレートを保存するために使用される 'Emails'ビューディレクトリを提供します。コンポーネントへ

変更:

  1. 変更public
  2. 変更public $layout = 'Emails';

  3. public $viewPath = '/Emails';に変更するには、すべての var宣言が _getBodyTextでパスを描画:

$body = $this->controller->render($this->viewPath . DS . 'text' . DS . $view, $this->layout . DS . 'text'.DS.'default');

  • 変更_getBodyHtmlパスをレンダリング
      は:

    $body = $this->controller->render($this->viewPath . DS . 'html' . DS . $view, $this->layout . DS . 'html'.DS.'default');

  • コメントアウト行:
  • $bodyText = $this->_getBodyText($view); $message->setBody($bodyText, "text/plain");

    HTML & TEXTの両方をアクティブに設定すると、Swiftmailerコンポーネントは空の電子メールを送信します。両方の電子メールビューから読み取ります&本文に本文を追加します。 htmlの電子メールを送信する場合は、この2行をコメントする理由です。

    第二の理由は、両方のは、あなたが両方email.html.ctp & email.text.ctpファイルの内容を持っている&活性化している場合、それは現実には両方のフォーマットがヘッダーに存在している(その内のヘッダの問題は、テキストのみの形式は、電子メールに表示されます作成しますが、それはhtmlの部分を抑制する&はテキストの部分を示す)。

    3

    右の設定は次のとおりです。だから、

    public $gmail = array(
        'host' => 'ssl://smtp.gmail.com', 
        'port' => 465, 
        'username' => '[email protected]', 
        'password' => 'secret', 
        'transport' => 'Smtp' 
    ); 
    

    輸送要素を忘れないでください。

    関連する問題