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.

Dani AI

Generated

A few focused checks will usually reveal why identical mail code works inside a controller but not when moved to a helper. The most likely causes are: the helper class never being loaded or executed, a silent failure from the mail transport (no exception shown), or the SMTP environment differing (port/encryption/auth or the mail server rejecting the message). posted working controller code and an almost-identical helper; the next step is to determine which of those three categories is happening.

Quick, practical checklist (run these in order):

  • Confirm the helper is actually invoked by adding a simple log entry at the start of the helper method or writing a short marker to the app log. If the marker never appears, the problem is autoload/import or filename/classname mismatch (case matters on Linux).
  • Make the helper return the mailer send result and log it. Wrap the send call in exception handling so transport errors get written to the log instead of being swallowed.
  • Check for class name collisions: a very generic class name can conflict with another extension. Temporarily rename the helper (for example, MailHelper) to rule this out.
  • Inspect the sending server: check the system mail logs (or ask the host) for rejections, and check the recipient mailbox (including spam).

Connectivity, SMTP and header issues:

  • Verify SMTP reachability from the server (telnet or openssl s_client). Many hosts block outbound port 25; common alternatives are 587 (STARTTLS) and 465 (implicit SSL). ’s hint about ports/SSL is relevant — confirm which port and encryption the SMTP provider requires and whether authentication is needed.
  • Ensure the From address and domain are valid and have appropriate SPF/DKIM records if using a remote SMTP; many MTAs silently drop or reject mail with local/invalid sender domains.

Ignore the off-topic torrent suggestion in the replies. The fastest path to a solution is to confirm the helper runs, capture the send result and any exceptions, and then check SMTP connectivity and server-side logs.

  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.