Wednesday, May 30, 2012

Sending email in yiiframework using yii-mail extensions

Sending email in a website is one of the main function to have interaction with the users and one of main features of the website.

For yii users maybe ever had error when sending the emails in the localhost with some kinds of warning.

The error maybe like this :

mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() 

It has caused because in the localhost there is no mail server so you can even send email. To solve this issue you must install mail server in your local computer such as hMailServer.

Or if you want to use the mail server from your provider you can use the SMTP configuration email from the provider.



For this time I will guide you to make implementation of sending email using yii-mail extention. First dowload the extension  here. And then you have to extract the extensions to  /protected/extensions/

You must config your main file like below code :


'components'=>array( 
          'mail' => array(
    'class' => 'ext.yii-mail.YiiMail',
    'transportType' => 'php',
    'viewPath' => 'application.views.mail',
    'logging' => true,
    'dryRun' => false,
If you want to use the email from the provider, you can use options such like host, username and password like this. In this case I use yahoo mail server.
'mail' => array(
    'class' => 'ext.yii-mail.YiiMail',
     'transportType'=>'smtp',
     'transportOptions'=>array(
       'host'=>'smtp.mail.yahoo.com',
       'username'=>'nama_email',//contohna nama_email@yahoo.co.id
       'password'=>'xxxx',
       'port'=>'465',
       'encryption'=>'ssl',
     ),
    'viewPath' => 'application.views.mail',
    'logging' => true,
    'dryRun' => false
) 
And then don't forget to configure your email sender in the main config files. /protected/config/main.php
'params'=>array(
        // this is used in contact page
        'adminEmail'=>'email_name@yahoo.co.id',
    ),
And then to send email you can use this code :

                Yii::import('ext.yii-mail.YiiMailMessage');
  $message = new YiiMailMessage;
  $message->setBody('Message content here with HTML', 'text/html');
  $message->subject = 'My Subject';
  $message->addTo('viyanatmail@gmail.com');
  $message->from = Yii::app()->params['adminEmail'];
  Yii::app()->mail->send($message);

For using the GMail you can configure the settings likes below :
Incoming Mail (POP3) Server - requires SSL: pop.gmail.com
Use SSL: Yes
Port: 995
Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
Account Name: your full email address (including @gmail.com or @your_domain.com)
Email Address: your email address (username@gmail.com or username@your_domain.com)
Password: your Gmail password
Note : Don't forget to activate the SMTP menu from your account

7 comments:

  1. how to activate the SMTP menu from your account

    ReplyDelete
  2. Search for the option in your mail account to activate SMTP menu.

    ReplyDelete
  3. Main Config file is
    'mail' => array(
    'class' => 'ext.yii-mail.YiiMail',
    'transportType' => 'smtp',
    'transportOptions'=>array(
    'host'=>'smtp.gmail.com',
    'encryption'=>'ssl', // use ssl
    'username'=>'nirav.motta@gmail.com',
    'password'=>'********',
    'port'=>465, // ssl port for gmail
    ),
    'viewPath' => 'application.views.mail',
    'logging' => true,
    'dryRun' => false
    ),

    controller file is
    Yii::import('ext.yii-mail.YiiMailMessage');
    $message = new YiiMailMessage;
    $message->setBody('hello how are you haaa','text/html');
    $message->subject = 'My Subject';
    $message->addTo('nerav.motta@gmail.com');
    $message->from = Yii::app()->params['adminEmail'];
    Yii::app()->mail->send($message);

    i get the error
    fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?)

    can you explain me to active smtp menu in yahoo account and what is the problem thank you

    ReplyDelete
  4. Search for php.ini and enable ssl to your webserver extension=php_openssl.dll

    http://stackoverflow.com/questions/2643462/setting-up-ssl-on-a-local-xampp-apache-server

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I follow this instruction but i have an error,please help me
    fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
    )

    ReplyDelete