We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

send swift email with phalcon

Hello guys, i am using swift to send an email using the Phalcon. But the problem i am facing is that the mail i am sending goes to the spam and the body is emply. Here is my code $this->getDI()->getMail()->send( array($app->email), 'Your tracking number is '.$app->tracking.' and provided full name is '.$app->firstname.' '.$app->lastname, 'confirmation', array( 'confirmUrl' => '/confirm/' . $app->tracking. '/' . $app->email ) );

Can anybody help now? Why the confirmurl does not appear on the email, only message 'Your trackin number....' appears as the subject of the email. Thank you in advance

Here is my aftercreate function in the Model public function afterCreate() { //send email $this->getDI()->getMail()->send( array($this->email), 'Your tracking number is '.$this->tracking.' and provided full name is '.$this->firstname.' '.$this->lastname, 'confirmation', array( 'confirmUrl' => '/confirm/' . $this->tracking. '/' . $this->email ) ); }



85.5k
Accepted
answer
edited Dec '15

Hello, and Merry Christmas,


$di->setShared('mails', function($subject, $to, $content){

    $transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
            ->setUsername('your email account')
            ->setPassword('your email password');

        $mailer = Swift_Mailer::newInstance($transporter);

        $message = Swift_Message::newInstance($subject)
          ->setFrom(array('from email ' => 'display name'))
          ->setTo( array($to => $to) )
          ->setBody($content, 'text/html');

        $result = $mailer->send($message);

        return $result;
});

and ccording to this https://docs.phalcon.io/en/latest/reference/di.html#resolving-services, you should be able to call

$component = $di->get("mails", array("subject", "reciever email", " conent of the messsage"));

i cannot test it right now, but it should be working.

Now in your model you can var_dump the conent, and call the function when you know the message isn't empty.

ALSO is better to use CLI application for sending mails and use queue for putting jobs.

Hello, Thank you for your help. But the mail issue here is the body of the email i am sending. According to $this->getDI()->getMail()->setBody("Thanks","text/html"); $this->getDI()->getMail()->send( array($this->email => $this->firstname), 'Your tracking number is '.$this->tracking, 'Confirmation', array( 'ConfirmUrl' => '/Confirm/'.$this->tracking, ) Which part is the body because only the subject is being sent "Your tracking number is". Any help? Thanks in advance

edited Jan '16

$this->getDI()->getMail()->send( array($this->email => $this->firstname), 'Your tracking number is '.$this->tracking, 'Confirmation', array( 'ConfirmUrl' => '/Confirm/'.$this->tracking, )

Is the correct codes in afterCreate() function in one of my controllers according to the tutorial in this link:
https://www.sitepoint.com/sending-confirmation-emails-phalcon-swift/

All i needed was to add this: require_once DIR . '/../../vendor/Swift/swift_required.php'; On top of my Model and it all worked using the codes you provided Jurijag. Thank you.