how to receive mail and send notification to the user in rails? - ruby-on-rails

I have been trying to receive emails in rails using IMAP and send a notification to user that new mail has come. I have a table namely "email" where i have to store the email information like message_from, message_to, message and i wanted to know how to fetch the emails from the gmail whenever a new mail comes in. And the following is the code
require 'net/imap'
require 'net/http'
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.login('sampleuser', 'password')
imap.select('INBOX')
imap.search(["NOT", "SEEN"]).each do |message_id|
emails = imap.fetch(mail,'RFC822')[0].attr['RFC822']
#mail = Email.new("from" => emails.from, "to" => emails.to)
#mail.save
end
but i cannot able to fetch the message_from or message_to or the message, it shows error as
TypeError (can't convert Symbol into Integer):
how can i get those details and i want to send notification to the user when a new entry is created in the table and clicking on that should take it to the page where i have to display the email.
How can i do this and should i be using something like scheduler to check if the new mail has come and if i am not reading the mail how can i identify that i have received the mail already and i do not want to duplicate. Please help me.

Your best bet on receiving emails is Griddler. It's easy to setup
You can send the email to the model you want and do the processing there.

Related

How imap.seqno works in nodejs?

My Node app is using imap module to retrieve emails:
var f = imap.seq.fetch('1:3', {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true
});
The app fetches emails with seqno 1-3 in gmail mail box. However I did not see those 3 emails in the mail box and only found them by searching the email subject. It seems the imap module retrieve some real old emails which are not visible in the mail box. I am wondering how seqno is issued in a mail box and weather a seqno for an email is unique and stay unchanged after issuing.

Identifying users in Rails Web push notifications

I'm developing a Rails application, and I'd like to send web push notifications to specific users when certain actions happen, e.g:
A user started tracking a timer, but the timer has been running for more than 6 hours. Then the app sends that user a web notification.
I've been doing research and found this tutorial, the author implements push notifications for Rails, however there's no insight on how to identify the users.
From what I understood, the users needs to subscribe from their browser to be able to get push notifications, however, considering each user can use the application from multiple browsers, how can I automatically subscribe/unsubscribe a user for notifications in all browsers they use the app from?
So, what I did was adding a notification_subscription model to my User model.
On my javascript, I check if there's a current browser subscription present:
this.serviceWorkerReady()
.then((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager.getSubscription()
.then((pushSubscription) => {
if (pushSubscription && _.includes(subscriptionEndpoints, pushSubscription.endpoint)) {
return;
}
this.subscribe();
});
});
I check if the current subscription is already present in the user stored endpoints, and subscribe if it isn't.
On subscription I send the new endpoint to the backend, which adds the new subscription to the user:
$.post(Routes.users_subscriptions_path({ format: 'json' }), {
subscription: subscription.toJSON()
});
Then I can send notifications to users to every endpoint:
def push_notifications_to_user(user)
message = {
title: "A message!",
tag: 'notification-tag'
}
user.notification_subscriptions.each do |subscription|
begin
Webpush.payload_send(
message: JSON.generate(message),
endpoint: endpoint,
p256dh: p256dh,
auth: auth,
api_key: public_key
)
rescue Webpush::InvalidSubscription => exception
subscription.destroy
end
end
end
The webpush gem raises an InvalidSubscription exception if the endpoint is invalid, we can destroy that endpoint to keep only the valid endpoints from the user.
The endpoint is unique by browser so you need an additional authentication scheme on the top of your app to send user's information along with the new endpoint.
You need to attach metadata (i.e. the user ID) to the endpoint when you store it on your server:
#subscription = Subscriptions.new endpoint: params[:endpoint]
#subscription.user = current_user
// or if you send with AJAX the user id together with the endpoint
#subscription.user = User.find params[:user_id]
In the second case I suggest to sign the user ID or use a secret token, otherwise anyone would be able to subscribe to push notifications as if it was another user.
Then you can delete from the database all the endpoints that belong to that user ID to unsubscribe all his devices.
However I don't think it's a good practice: a user may want to receive notifications on a device and not on another one.

Twilio check if phone number has been blacklisted

I am currently integrating into the twilio rest api and need to perform a check on a users phone number to determine if that user has blacklisted themselves or not. I have little experience with this api and scouring through the documentation and google has turned up nothing.
In our application we are going to have a notification center and if the user has blacklisted themselves I do not want to give them the ability to turn on their SMS notifications. Potentially a user could have SMS notifications on but twilio would block any messages. I know there is the ability to get a status code back from twilio when an SMS is queued that shows the user is blacklisted (https://www.twilio.com/docs/api/rest/message). However, I will not be sending messages on the notifications screen and need a direct way (if at all possible) to check twilio to determine if a number is blacklisted. Any help is much appreciated. Let me know if anymore information will be of help.
Megan from Twilio.
I'd be curious to see if you ever tried your own workaround. But I wanted to note for others in a similar situation how you could grab the blacklist error and then do whatever you may want with it.
In Ruby it would look something like this:
require 'rubygems'
require 'twilio-ruby'
account_sid = 'YOUR_ACCOUNT_SID'
auth_token = 'YOUR_AUTH_TOKEN'
#client = Twilio::REST::Client.new account_sid, auth_token
begin
#message = #client.messages.create(
from: 'TWILIO_NUMBER',
to: 'USER_NUMBER',
body: 'Howdy!'
)
rescue Twilio::REST::RestError => e
if e.code == 21610
# User is blacklisted
# Store info however you choose
puts e.message
end
end
We check for blacklisting specifically using the code '21610'. For more information about errors you can visit the reference page.
Hope this helps!
Twilio recommends developers to store the opt-out/in statuses in their side. I have stored it in DB. There are 2 ways to collect the unsubscribed users list.
1) Use SMS webhooks. You can find how to configure your Twilio number to receive webhook events here
#PostMapping(value = "/twilio", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_ATOM_XML_VALUE)
public String twilioConsumer(TwilioEventDTO twilioEventDTO) {
// twilioEventDTO.getBody() => returns the body of the SMS user replied.
twilioService.consume(twilioEventDTO);
return new MessagingResponse.Builder().build().toXml();
}
2) Since I implemented webhooks later, I had to collect already unsubscribed users. When you send sms to the number that has been opted-out, Twilio API throws an exception with the status number of 21610. You can catch it and store the number in DB.
try {
Message result = Message.creator(
new PhoneNumber(toPhoneNumber),
new PhoneNumber(fromPhoneNumber),
messageBody)
.create();
response = result.getStatus().name();
} catch (ApiException e) {
if (e.getCode().equals(21610))
updateSubscription(toPhoneNumber, false);
logger.warn("Error on sending SMS: {}", e.getMessage());
}
P.S.: examples written in Java - Spring Boot framework.

Send an email from my app without using MFMailComposeViewController

Is it possible to send an automatically generated email using my own email address to another email address of mine by clicking a button?
I tried to use MFMailComposeViewController but another view appeared. Can I do it without this view?
You can do it only by creating own server-side mailer. On button clicking you have to send request with all needed data (email address, body, subject, etc) and server will send mail.
If You want send directly from app - MFMailComposeViewController is the only LEGAL way
By default in iOS you can only use the MFMailComposeViewController, which uses the user's mail account. Therefore you cannot send fully automated mail messages (the user allways has to confirm/cancel).
libMailCore is a great iOS framework which allows you to generate and send mails without any user interferance. In that case you'll be using your own server/credentials (thus not the user mail account). There are apps in the App Store using mailcore, so i would guess it's legit.
Yes there is a way using Swift-SMTP.
Send email
Create a Mail object and use your SMTP handle to send it. To set the sender and receiver of an email, use the User struct:
let drLight = Mail.User(name: "Dr. Light", email: "drlight#gmail.com")
let megaman = Mail.User(name: "Megaman", email: "megaman#gmail.com")
let mail = Mail(
from: drLight,
to: [megaman],
subject: "Humans and robots living together in harmony and equality.",
text: "That was my ultimate wish."
)
smtp.send(mail) { (error) in
if let error = error {
print(error)
}
}

how to share a link via sms in blackberry

I am building an application where I need the option to share via email and SMS.
I have done the share via Email, where when the user selects the image, the url is passed as the content of the email. But while sharing via SMS, I can't do something like setContent as I did for email and fetch the url in the SMS directly, instead of user typing the address manually.
I am using Message class in email and MessageConnection class for SMS, as shown in the blackberry community example.
The Message object you receive when calling MessageConnection.newMessage(TEXT_MESSAGE) is actually a TextMessage object (or a BinaryMessage object with BINARY_MESSAGE).
If you cast the received object to the proper class (TextMessage or BinaryMessage), you should be able to use its setPayloadText(String data) (or setPayloadData(byte[] data) for a BinaryMessage) to enter a value in the message before sending it.
Your code should look like this:
Message msg = myMessageConnection.newMessage(TEXT_MESSAGE, /* address */);
TextMessage txtMsg = (TextMessage)msg;
txtMsg.setPayloadText(/* Text to send */);
myMessageConnection.send(msg);
When you send an email, you can set the body of it and send it to the user from the Email native application. You cant do taht for SMSs. I worked on that issue and for BB Torch I was able to set the text of the SMS message but for other devices that was impossible. I always obtain an empty text message!!
So y suggestion to you is using the following code wich will send the SMS to a number without the interference of the user
MessageConnection conn = (MessageConnection) Connector.open("sms://" + userNumber);
TextMessage txtmessage = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
txtmessage.setPayloadText(text);
conn.send(txtmessage);

Resources