I'm using Yii framework while my question is probably intended to PHP expert. I have created a controller to send email from my web application, it works fine.

Given that, I intend to use email in several sontrollers in my app, I wanted to created a helper but that does not work. Email is not sent. (I'm using swiftmailer)

The code of the working controller is the following:

<?php

class MailController extends Controller
{
    /**
     * Declares class-based actions.
     */
    public function actionSendemail() {

    // Plain text content
    $plainTextContent = "This is my first line ;-)\nThis is my second row of text";

    // Get mailer
    $SM = Yii::app()->swiftMailer;

    // New transport      mailHost= localhost,    mailPort = 25
    $Transport = $SM->smtpTransport(Yii::app()->params['mailHost'], Yii::app()->params['mailPort']);

    // Mailer
    $Mailer = $SM->mailer($Transport);

    // New message
    $Message = $SM
        ->newMessage('My subject')
        ->setFrom(array('test1@localhost.localdomain' => 'Example Name'))
        ->setTo(array('myemail@domain.com' => 'Recipient Name'))
     //   ->addPart($content, 'text/html')
        ->setBody($plainTextContent);

    // Send mail
    $result = $Mailer->send($Message);

}
}

The helper code is the following:

<?php
// protected/components/Email.php

class Email {
    public static function sendEmail($subject, $from, $to, $body)
    {
        // Get mailer
        $SM = Yii::app()->swiftMailer;
        // New transport
        $Transport = $SM->smtpTransport(Yii::app()->params['mailHost'], Yii::app()->params['mailPort']);
        // Mailer
        $Mailer = $SM->mailer($Transport);
        // New message
        $Message = $SM
            ->newMessage($subject)
            ->setFrom(array($from => 'Example Name'))
            ->setTo(array($to => 'Recipient Name'))
         //   ->addPart($content, 'text/html')
            ->setBody($body);

        // Send mail
        $result = $Mailer->send($Message);
    }
}

the way I call it is the following:

$subject= 'My subject';
$from = Yii::app()->params['adminEmail'];   // adminEmai is a globalparam like above controller
$to='xxxx@xxx.com';
$body='my body';
Email::sendEmail($subject, $from, $to, $body);

when I run this code, I have no error, but I dont receive the email.

Thank you for your help.

  1. change the port. I remember that than port 25 can be used.
  2. set your header so that receiver server should not block it.
  3. is it ssl enabled ?
  4. can use libaray for this pupose.
  5. find torrent for [ 1054 Must Have PHP Scripts of 2008 ]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.