Upgrading to Rails 5 and rspec 3.7 no longer sends emails via ActionMailer::Base.deliveries - email

I had a functional spec suite using rspec with a Rails 4.2.8. I upgraded to Rails 5.0.6 and rspec 3.7 accordingly.
Many of the specs work, but none of my specs that send emails actually add the email to the ActionMailer::Base.deliveries array.
I've verified that config/environments/test/rb has config.action_mailer.delivery_method = :test, which should add any sent emails to the array instead of actually sending them.
I suspect it has something to do with ActiveJob as well, because when I send an email via SomeMailer.some_method().deliver_later in my specs, it returns an ActionMailer::DeliveryJob object, but it does not add anything to the ActionMailer::Base array.
When I call SomeMailer.some_method().deliver_now, I find the emails actually are added to the ActionMailer::Base array. I'm wondering if for my specs I just need to configure them to all use deliver_now instead of deliver_later?
I've reviewed and followed the Ruby on Rails upgrade guide from 4.2 -> 5.0 and can't figure this out.
My specs with emails follow the format:
# spec/models/record_spec.rb
require "rails_helper"
describe Model do
describe "#method" do
it "sends email" do
record = Record.create(attributes = {})
RecordMailer.welcome_email(record).deliver_later
last_email = ActionMailer::Base.deliveries.last
expect(last_email.subject).eq ("Welcome")
end
end
end

Assuming you're including ActiveJob::TestHelper somewhere, you can wrap your deliver_later (or if in a feature spec, for example, a button click triggering the delivery) inside a perform_enqueued_jobs block:
perform_enqueued_jobs do
RecordMailer.welcome_email(record).deliver_later
end
Now your ActionMailer::Base.deliveries should contain the email sent via ActiveJob.

Related

Thunderbird WebExtensions / MailExtensions development - How to deal with events such as "new mail"?

I'm trying to write my very first Thunderbird extension. If possible, I'd like to only use the newer WebExtensions / MailExtensions APIs.
Two things my extension needs to do:
Performs an action when a new mail arrives and is not junk.
When a message is read, check if there are still unread messages and, if not, performs an action.
The only examples I've found online dealing with "new mail event" hooks look like there are not using the newer APIs. For example:
Components.classes["#mozilla.org/messenger/msgnotificationservice;1"]
.getService(Components.interfaces.nsIMsgFolderNotificationService);
notificationService.addListener(myListener, notificationService.msgAdded);
or
Components.classes['#mozilla.org/messenger/services/session;1']
.getService(Components.interfaces.nsIMsgMailSession)
.AddFolderListener(myListener, Components.interfaces.nsIFolderListener.all);
... where myListener would be called when a new email arrives.
Those codes generate the error Components.classes is undefined in Thunderbird 91. If I understand properly this is because more stuff is required to stay compatible with the legacy API.
My question:
What is the proper way to listen to a new email event, using the WebExtensions / MailExtensions APIs?
Links I did read (but maybe I missed something!):
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Developing_WebExtensions_for_Thunderbird
https://webextension-api.thunderbird.net/en/91/
Oh! I found it!
background.js :
browser.messages.onNewMailReceived.addListener((folder, messages) => {
// ...
});
Those permissions are required: messagesRead and accountsRead.

Send an email when a boolean is true

I am trying to send an email whenever a boolean value equals. The email needs to contain info from a list that is created in a groovy script earlier in the job. whenever this list isn't empty I will need to create a text/HTML email with the contents of the list.
currently I have the email extension plugin but I can't find a way to integrate it with what I need. Is there anyway I could send the email using groovy or use a plugin that triggers based on what I need?
To anyone who it may concern, I discovered that with the Flexible Publish Plug in you can add conditionals to your post build actions, easiest to use string values and just compare those. this is because you can set up parameters at the start of your build that you plan to use to store info in the build environment, and it can be accessed from other places.
you can set string params using the following code:
def paramTempHolder = new StringParameterValue('PARAM', 'desired value')
build.replaceAction(new ParametersAction(paramTempHolder))
for me I used send to indcate I needed to send my email so my code read:
def paramTempHolder = new StringParameterValue('SendEmail', 'send')
I then used $SendMail as string 1 in flexible publish and just send as string 2. If the condition is meet it will send my email. I can use the same parameter manipulation to get the info I need into my email so that it sends like I want it to.
EDIT: I forgot to mention that inorder to use the replaceAction method you will need to add the following import to your script:
import hudson.model.*

Send mails from Notes - when it is not used for mail

Ok, in these times when some people move from Lotus Notes to Office 365 I have come across a certain requirement...
An older workflow application sends mail to users. This has worked fine for ages. But now we have a new type of users. These users are just using Notes for a couple of old legacy applications like the one in question.
The error we get is:
File does not exist
And the code that generates it is pretty simple:
Dim session As New NotesSession
Dim db As NotesDatabase
Dim rtitem As NotesRichtextItem
Dim doc2 As NotesDocument
Set db = session.CurrentDatabase
Set doc2 = New NotesDocument(db)
doc2.Form = "Memo"
doc2.Subject = "Test mail " & now
Set rtitem = New NotesRichTextItem (doc2, "Body" )
Call rtitem.AppendText("A simple test....")
Call rtitem.addnewline(2)
Call rtitem.AppendText("Link to complaint ")
Call doc2.Replaceitemvalue("sendto", "john#dalsgaard-data.dk")
doc2.Send( False )
It fails when running the last line....
So, the question really is: How can I code around the this issue?
I know there is no mail file for the user - and I would really prefer not to have to create one for the new users.
Thanks in advance!
/John
Error will appear when user triggering the code doesn't have a mail file specified in person document/location. One option would be to change code to save the new mail directly to server mail.box (assuming server is configured to route mails) or just send the email directly through SMTP using java.
Ok, I have done some trial & error testing on this....
Conclusion so far is that it works in this situation:
In the LOCAL location document (in names.nsf on the computer) you specify:
Mail server - as a server the user can reach
Mail "On Server"
An existing mail file - it can be ANY existing file on the server, it doesn't have to be a mail database.
Actually, a non-conclusive test indicates the mail database even doesn't have to exist (but the user with the setup for testing had to leave - so I couldn't confirm this tonight...)
Edit:
Further testing indicates that this may not be a problem if the user is NOT roaming. I need some further verification that this is actually the reason why I got it working (for one thing you cannot remove the mail file name again once added)... But thought I would add it here.

how to send files as attachment in email in magento for downloadable products?

I am new to Magento. I am working on a web site which is selling downloadable products.
My client want purchased products should be sent via email in attachment?
Currently, I am developing in localhost so I am not sure whether magneto actually send product files in email or not?
Should I need to enable any option for that in configuration?
If you want to develop this at your localhost keep in mind, that you have to set up mail server or use some solutions from community like below link, to send it by gmail and other:
https://www.magentocommerce.com/magento-connect/smtp-pro-email-free-custom-smtp-email.html
You also can just configure your server to save mails without sending.
So you'll be able to review it.
Good article about how to send downloadable by email here:
https://magento.stackexchange.com/questions/49511/how-can-i-get-only-the-downloadable-product-url-in-email-template
But!!! keep in mind that the latest Magento use queue to send email (which runs by cron), so if you have such a distribution (for example 1.9+) you need to adjust code which you have from link above.
Here istwo solutions for this case:
Disable adding email to queue after order.
Just comment this part of code in "Mage_Core_Model_Email_Template"'s "send()" method:
// if ($this->hasQueue() && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {
/** #var $emailQueue Mage_Core_Model_Email_Queue */
/* $emailQueue = $this->getQueue();
$emailQueue->clearRecipients();
$emailQueue->setMessageBody($text);
$emailQueue->setMessageParameters(array(
'subject' => $subject,
'return_path_email' => $returnPathEmail,
'is_plain' => $this->isPlain(),
'from_email' => $this->getSenderEmail(),
'from_name' => $this->getSenderName(),
'reply_to' => $this->getMail()->getReplyTo(),
'return_to' => $this->getMail()->getReturnPath(),
))
->addRecipients($emails, $names, Mage_Core_Model_Email_Queue::EMAIL_TYPE_TO)
->addRecipients($this->_bccEmails, array(), Mage_Core_Model_Email_Queue::EMAIL_TYPE_BCC);
$emailQueue->addMessageToQueue();
return true;
}*/
So emails will be sent immediately (be aware, that if you have very big turnover with spikes it can create performance issue)
Second way is to save full path of attachment to table "core_email_queue" field - "message_parameters". You can do it adding url to array of argument here $emailQueue->setMessageParameters( in code above.
After that you can handle it in "Mage_Core_Model_Email_Queue"'s "send()" method
using standard "Zend Mail"'s method - "createAttachment()". Below link provides deeper explanation of this part.
https://magento.stackexchange.com/questions/9652/magento-send-file-attachements-in-emails
Hope it will help sombody.
Have a good day!!!

What is the best way to log errors in Zend Framework 1?

We built an app in Zend Framework (v1) and have not worked a lot in setting up error reporting and logging. Is there any way we could get some level or error reporting without too much change in the code? Is there a ErrorHandler plugin available?
The basic requirement is to log errors that happens within the controller, missing controllers, malformed URLs, etc.
I also want to be able to log errors within my controllers. Will using error controller here, help me identify and log errors within my controllers? How best to do this with minimal changes?
I would use Zend_Log and use the following strategy.
If you are using Zend_Application in your app, there is a resource for logging. You can read more about the resource here
My advice would be to choose between writing to a db or log file stream. Write your log to a db if you plan on having some sort of web interface to it, if not a flat file will do just fine.
You can setup the logging to a file with this simple example
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 4
Also, I would suggest sending Critical errors to an email account that is checked regularly by your development team. The company I work for sends them to errors#companyname.com and that forwards to all of the developers from production sites.
From what I understand, you can't setup a Mail writer via a factory, so the resource won't do you any good, but you can probably set it up in your ErrorController or Bootstrap.
$mail = new Zend_Mail();
$mail->setFrom('errors#example.org')
->addTo('project_developers#example.org');
$writer = new Zend_Log_Writer_Mail($mail);
// Set subject text for use; summary of number of errors is appended to the
// subject line before sending the message.
$writer->setSubjectPrependText('Errors with script foo.php');
// Only email warning level entries and higher.
$writer->addFilter(Zend_Log::WARN);
$log = new Zend_Log();
$log->addWriter($writer);
// Something bad happened!
$log->error('unable to connect to database');
// On writer shutdown, Zend_Mail::send() is triggered to send an email with
// all log entries at or above the Zend_Log filter level.
You will need to do a little work to the above example but the optimal solution would be to grab the log resource in your bootstrap file, and add the email writer to it, instead of creating a second log instance.
You can use Zend_Controller_Plugin_ErrorHandler . As you can see on the documentation page there is an example that checks for missing controller/action and shows you how to set the appropriate headers.
You can then use Zend_Log to log your error messages to disk/db/mail.