[EXTENSION] Mailer using PHPMailer

Hi,

this is certainly a silly question:

I want to use a helper function or class to send an Email with Emailer extension, because I dont want to configure the mailer within the controller every time.

Where is the best place for it? How to extend the component extension correctly?

the function is something like this: function sendMail($to, $subject, $body){}

Quote

Hi,

this is certainly a silly question:

I want to use a helper function or class to send an Email with Emailer extension, because I dont want to configure the mailer within the controller every time.

Where is the best place for it? How to extend the component extension correctly?

the function is something like this: function sendMail($to, $subject, $body){}

You can find and example in the documentation tab of the extension.

this is my approach to configure the Emailer

  • create a class MyApp in protected/helpers folder
  • create a function MyApp::sendMail($to, $subject, $body)
  • import application/helpers.*
  • added the Emailer to application components

Anyone know how to get this to work with views in a theme?

Currently I have to copy the views to the views folder in the protected directory.

Cheers,

Sn0rcha

Hello this extesion is amazing…

But How can I do to send a e-mail with images ?? I mean that the content of the e-mail contains images. (Not attachments).

I research and I need to specify some headers… but using this extension how can I do that?





'components'=>array(

                'mailer' => array(

                          'class' => 'application.extensions.mailer.EMailer',

                          'pathViews' => 'application.views.email',

                          'pathLayouts' => 'application.views.layouts'

                       ),




this wont change the


   

protected $pathLayouts = 'application.views.email.layouts';



i ve to change it manualy on the emailer.php

is there any mistake?

Hey guys, I’m trying to set up so Mailer works with the Godaddy account together. I have been so far unsuccessful. Can anybody let me know what is wrong in my configuration?


Yii::app()->mailer->Host = 'smtpout.secureserver.net';

		Yii::app()->mailer->IsSMTP(true);

		Yii::app()->mailer->Username = "info@****.com";

		Yii::app()->mailer->Password = "****";

		Yii::app()->mailer->From = 'info@****.com';

		Yii::app()->mailer->FromName = '****';

		Yii::app()->mailer->IsHTML(true);

		Yii::app()->mailer->CharSet = 'UTF-8';

		Yii::app()->mailer->AddReplyTo('info@****.com');

		Yii::app()->mailer->AddAddress($********->email);

		Yii::app()->mailer->Subject = '****';

		Yii::app()->mailer->Body = $message;

		if(!$mailer->Send())

		{

			$this->actionBad();

			throw new CHttpException(500, 'Something went horribly wrong');

		}

		else

		{

			$this->actionOk();			

		}



I get: "SMTP Error: Could not connect to SMTP host. Transaction Failed!"

I have enabled SMTP relay on the account and I still get this problem. :(

PS: one more question, is there a way of connecting with SSL using this extension?

EDIT:

Made it work - just had to put "relay-hosting.secureserver.net" into host part of the setup.

Continuing to work on SSL setup.

EDIT:

getting connection refused. I’m using 465 port, SMTPSecure = ‘ssl’ SMTPAuth = True.

I used this extension on one of my previous projects, and ssl connection works fine so far with these configs:




            $this->SMTPDebug = false;

            $this->Mailer='smtp';

            $this->SMTPAuth=true;

            $this->SMTPSecure='ssl';

            $this->Host="smtp.gmail.com";

            $this->Port=465;




            $this->CharSet = 'utf-8';

            $this->ContentType = 'text/html';

            $this->WordWrap = 70;

            $this->FromName=Yii::app()->name;

            $this->setFrom=Yii::app()->params->siteEmail;

             $this->Username =  // SMTP username

            $this->Password = // SMTP password



I used this extension on one of my previous projects, and ssl connection works fine so far with these configs:




            $this->SMTPDebug = false;

            $this->Mailer='smtp';

            $this->SMTPAuth=true;

            $this->SMTPSecure='ssl';

            $this->Host="smtp.gmail.com";

            $this->Port=465;




            $this->CharSet = 'utf-8';

            $this->ContentType = 'text/html';

            $this->WordWrap = 70;

            $this->FromName=Yii::app()->name;

            $this->setFrom=Yii::app()->params->siteEmail;

             $this->Username =  // SMTP username

            $this->Password = // SMTP password



i tried this but I get




SMTP -> ERROR: Failed to connect to server: Connection refused (111)

SMTP Error: Could not connect to SMTP host.

Transaction Failed! 



My settings:





Yii::app()->mailer->SMTPDebug = true;

Yii::app()->mailer->Mailer='smtp';

Yii::app()->mailer->SMTPAuth=true;

Yii::app()->mailer->SMTPSecure='ssl';

Yii::app()->mailer->Host="smtp.gmail.com";

Yii::app()->mailer->Port=465;




Yii::app()->mailer->From = 'info@*******.com';

Yii::app()->mailer->FromName = '****';

Yii::app()->mailer->setFrom('info@****.com');

Yii::app()->mailer->AddReplyTo('info@****.com');*/

Yii::app()->mailer->AddAddress($client_email->email); 


Yii::app()->mailer->ContentType = "text/html";

Yii::app()->mailer->CharSet = 'UTF-8';


Yii::app()->mailer->Subject = '****';

Yii::app()->mailer->Body = $message;  


Yii::app()->mailer->Username = "****@gmail.com";

Yii::app()->mailer->Password = "****";


if(!Yii::app()->mailer->Send())

{

	$this->actionBad();

	throw new CHttpException(500, 'Something went horribly wrong');

}

else

{

	$this->actionOk();					

}




EDIT: DUH!!! Talked to guys in chat and figured out that I need to use SMTPS instead of SMTP. WOrks perfectly now.

Thanks!!!

Hi Guys,

Apologies in advance if someone has already raised this, but there is a small logic error in the phpmailer class on line 1236 in the addAttachments function.

The last element of the array is set to 0, this is the unique id for the attachment, therefore it will stop any subsequent attachments from being added by the AttachAll function.




      $this->attachment[] = array(

        0 => $path,

        1 => $filename,

        2 => $name,

        3 => $encoding,

        4 => $type,

        5 => false,  // isStringAttachment

        6 => 'attachment',

        7 => 0, // <= THIS LINE HERE

      );



If you change it to increment you can send more than 1 attachment per email.




      $this->attachment[] = array(

        0 => $path,

        1 => $filename,

        2 => $name,

        3 => $encoding,

        4 => $type,

        5 => false,  // isStringAttachment

        6 => 'attachment',

        7 => count($this->attachment),

      );



The error is present in the current version of the mailer extension.

Cheers

Ben

I tried to include the layout in my mailing function but unable to do.Here I am getting the mail with out layout.

this is my code…

in config main file


'mailer' => array(

                'class' => 'application.extensions.mailer.EMailer',

                'pathViews' => 'application.views.email',

                'pathLayouts' => 'application.views.email.layouts'

 ),

in my controller i using this code for sending the mail…

// Reading the content of mail from view…


$html = $this->renderPartial('//email/email',array(),true);

             Yii::app()->mailer->Body = $html;

            Yii::app()->mailer->Send();

AddAttachment function is sending one attachment only, though we add multiple attachments

Actual code is




public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {

    try {

      if ( !@is_file($path) ) {

        throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);

      }

      $filename = basename($path);

      if ( $name == '' ) {

        $name = $filename;

      }


      $this->attachment[] = array(

        0 => $path,

        1 => $filename,

        2 => $name,

        3 => $encoding,

        4 => $type,

        5 => false,  // isStringAttachment

        6 => 'attachment',

        7 => 0

      );



not working as it always sets 0 to 7th index of property ‘attachment’

It worked if i set to rand()




public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {

    try {

      if ( !@is_file($path) ) {

        throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);

      }

      $filename = basename($path);

      if ( $name == '' ) {

        $name = $filename;

      }


      $this->attachment[] = array(

        0 => $path,

        1 => $filename,

        2 => $name,

        3 => $encoding,

        4 => $type,

        5 => false,  // isStringAttachment

        6 => 'attachment',

        7 => rand()

      );



I didn’t listen Ben Mazzarol’ comment.

If anyone wants to send more than one attachment go with his coding which is better than mine.


count($this->attachment)

I am getting the error "Could not instantiate mail function". Does anyone know what might cause this or how to fix it?

i am using yii codes in godaddy site,i want send email through smtp server in our site but could not connect google smtp server please help me.

my codes

‘components’=>array(

 'mailer' =&gt; array(


  'class' =&gt; 'application.extensions.mailer.EMailer',


  'pathViews' =&gt; 'application.views.email',


  'pathLayouts' =&gt; 'application.views.email.layouts'

),)

public function actionmailsend() {

	&#036;mailer = Yii::createComponent('application.extensions.mailer.EMailer');


 &#036;mailer-&gt;IsSMTP();


 &#036;mailer-&gt;IsHTML(true);


// &#036;mailer-&gt;SMTPDebug  = 2;


 &#036;mailer-&gt;SMTPAuth = true;


 &#036;mailer-&gt;SMTPSecure = &quot;ssl&quot;;


 &#036;mailer-&gt;Host = &quot;smtp.gmail.com&quot;;


 //&#036;mailer-&gt;Host = 'ssl://smtp.gmail.com:465';


 &#036;mailer-&gt;Port = 465;   


 &#036;mailer-&gt;Username = &quot;xxxx@gmail.com&quot;;


 &#036;mailer-&gt;Password = &quot;xxxx&quot;;


 &#036;mailer-&gt;From = &quot;from@gmail.com&quot;;


 &#036;mailer-&gt;FromName = &quot;Test&quot;;


 &#036;mailer-&gt;AddAddress(&quot;example@gmail.com&quot;);


 &#036;mailer-&gt;Subject = &quot;Someone sent you an email.&quot;;


 &#036;mailer-&gt;Body = &quot;Hi, This is just a test email using PHP Mailer and Yii Framework.&quot;;


 


 &#036;mailer-&gt;Send();





}

my error is :could not connect smtp server