Phalcon, Send mail in background process - phalcon

I using phalcon framework,
I need send email (in background process) after submit contact form
Everyone help me with the demo code.
thanks all.

No can't help you with demo code. But can help you with the steps:
Prepare mail message
Put into into queue
In cli task(which will be fired by cron) get messages from queue and send it

You don't need any Phalcon CLI tasks or any cron job. You can just send your mail directly. You can use the phalcon incubator or this extension to easily add the mailer feature into your Phalcon project.
Based on your level, I suggest you to use the default operating system mailer which is supposed to be able to handle queuing in a native way so you don't have to bother developing your own cli task queue manager within your application.
The basic sendmail service will automatically queue the messages and try to send it again later if something went wrong.
$config = [
'driver' => 'sendmail',
'sendmail' => '/usr/sbin/sendmail -bs',
'from' => [
'email' => 'example#gmail.com',
'name' => 'YOUR FROM NAME'
]
];

Related

Messages Patterns - RabbitMQ/NestJS

I'm trying to integrate to a project done using NestJS, a simple api where you can publish messages with a name (or pattern) and send it to that system that has implemented a handler that matches the name.
The system I'm building is really small, it wouldn't make much of a sense using NestJS for that.
The problem I'm having is the following:
I'm creating a simple api that triggers the publish of the message onto a queue.
The consumer is on a system using NestJS.
I can't figure out how to give that messages a pattern that is recognized by that system.
For example:
Let's say I want to publish a message that has a name of "CreateRecord" with a payload to be processed from the other system that has a handler with the same name with an implementation.
Using amqplib how do I give messages a name or pattern?
You can provide a pattern in the content object when you are publishing a message.
publish(
'my_exchange',
'routing_key',
{ pattern: 'CreateRecord', data: 'Record' },
);

Using Laravel's Mailgun driver, how do you (gracefully) send custom data and tags with your email?

So I'm using Laravel 5.1 and trying to integrate with Mailgun. Well, that's easy, but now I'm trying to send custom variables from my application along with my emails.
I'm actually switching our application over from Mandrill because of their "new direction" and such. With them, I could supply the variables and tags via the email headers, but with Mailgun, that only works when you send via SMTP. In Laravel, Mail::send() uses an API call, so in theory I'd add the metadata there with "v:my-custom-data" => "{"this_id": 123}", but I'd like to avoid altering core classes like that.
I also considered using Bogardo/Mailgun, but then I'd have to replace all the Mail::send()s with Mailgun::send(), and then I couldn't send emails locally (environment based email driver), and then the application would be "married" to Mailgun.
Anyone done this before? Please let me know if I'm not clear here.
I fixed my own problem. I was wrong, YOU CAN add custom variables via the SMTP method:
// Send email with custom variables and tags in Laravel
Mail::send('emails.blank',
['html' => 'This is a test of Mailgun. <strong>How did it work out?</strong>'],
function($message) {
$message->to('jack#mailinator.com');
$message->subject('Mailgun API Test');
$headers = $message->getHeaders();
$headers->addTextHeader('X-Mailgun-Variables', '{"msg_id": "666", "my_campaign_id": 1313}');
$headers->addTextHeader('X-Mailgun-Tag', 'test-tag');
});
My testing was just inadequate. Very good to know, however I think this is an undocumented feature, so I suggest using with caution.
Update for Laravel 5.4+
As you can read in the offical docs:
The withSwiftMessage method of the Mailable base class allows you to register a callback which will be invoked with the raw SwiftMailer message instance before sending the message. This gives you an opportunity to customize the message before it is delivered:
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$this->view('emails.orders.shipped');
$this->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('Custom-Header', 'HeaderValue');
});
}
You can just do like this in Laravel 5 :
Mail::send(['text' => 'my_template'], $data, function ($message) {
..
$message->getSwiftMessage()->getHeaders()->addTextHeader('X-Mailgun-Tag', 'my-tag');
});
Laravel implementation of the Mailgun Driver doesn't support options/parameters.
Here's a Gist which adds support to the native driver : https://gist.github.com/j0um/27df29e165a1c1e0634a8dee2122db99

How to remove exception in Yii redis extension if one server down?

I use php (Yii), use this extension: http://www.yiiframework.com/extension/rediscache/ for saving yii cache and session in redis.
I configured it to work with 2 servers.
'cache'=>array(
'class'=>'application.extensions.redis.CRedisCache',
//if you dont set up the servers options it will use the default one
//"host=>'127.0.0.1',port=>6379"
'servers'=>array(
array(
'host'=>'10.1.98.139',
'port'=>6379,
),
array(
'host'=>'10.56.192.5',
'port'=>6379,
)
),
),
How I understand, Yii send cache and session data in both of them.
When one of them down, I need, that all users work in 1 of them, but Yii makes an Exception: 'Predis_CommunicationException' with message 'Connection refused' in /srv/www/protected/extensions/redis/Predis.php:1303
How to fix this, and work only on one of them?
Thanks!
As far I see you need to locate the code in the extension that handles the connection section, and setup two things:
a timeout of 1 second
a try/catch block or other way to handle cannot connect errors.
This should be done by the extension maintainer and you, and it looks like a undocumented behavior of the extension, so make sure you submit a comment/bug ticket to that extension.

First time mass-emailing using Sendgrid with rails

I am running a rails app on heroku and would like to send an email to 160 users. This is the first time I am doing this so I would to very whether the method below will lead to a successful outcome.
Sendgrid is all sent up and I have a controller setup that executes the following:
#users = User.all
#users.each do |u|
Email.send_email(p).deliver
end
I am assuming that since the number of recipients is relatively low I would be able to get by without using delayed_job or some other background processing.
SendGrid actually makes it easy to send out emails without having to use a background worker. You can do it using the X-SMTPAPI header and setting an array of email addresses in the to field. For example:
X-SMTPAPI: {
to: ["john.doe#example.com", "jackson#example.com", "freddy#example.com"]
}
In this example, each of these three emails will receive a separate copy of the email. No background workers, no complexity.
There's a gem called sendgrid that does a good job of adding some useful helpers to action mailer. Have a look at the "multiple recipients" section of the README
https://github.com/stephenb/sendgrid
I would advise that you invest the time into some background processing, as this could potentially be a hit or miss, all depending on the emailing service.

Rails 3.2 Serialize & Deserialize Mail

I'm trying to save email to a database, to send it later via a rake task. It was pretty easy in rails 2.3.8 (TMail), but I'm having trouble with rails 3.2.
Once I get the Mail object (mail), I call mail.encoded to serialize the email. I save this to the database.
My rake task loads the encoded message, but I can't find a way to recreate the mail object and call deliver (deserialize).
Mail.new(mail.encoded) seems like it should work, but the delivery fails because Mail.new doesn't get the default ActionMailer SMTP settings.
Anyone else doing something like this?
Thanks!
Alright, got this working.
To serialize the email I do the following.
email = mail(:to => 'to#me.com', ....)
string = mail.encoded
# later one
mail.new(email.string)
Mail.deliver(mail)
This appears to skip any HTML validation.
I can't set the Mail send settings (SMTP, etc), but I think it defaults to :sendmail, and that's working on the web server.