Collect data from email message - php

I want to collect data from email to mysql database using php.
If some one is sent a mail to their mail account to my mail id. I want that mail information to store in my database. for further operation. It is possible in PHP because I saw this feature in one hosting support application Kayako Fusion which developed by PHP.
So plese give some information to do this task.

If you want to parse with PHP the best way is to use a 3rd party API to revive the email and then send it to your PHP script with a HTTP Post.
I'd recommend using either sendgrid.com or mailgun.com
Both of these services will parse the email and send you the information in a http POST that you can then insert into mysql. API docs for both: MailGun Incoming Parse API / SendGrid Incoming Parse API

You can use the imap functions, for example:
<?php
$imap = imap_open("{server.example.com:143}INBOX" , 'login' , 'password');
if( $imap ) {
//Check no.of.msgs
$num = imap_num_msg($imap);
//if there is a message in your inbox
if( $num >0 ) {
//read that mail recently arrived
$the_message = imap_body($imap, $num);
//Do the stuff with $the_message
}
//close the stream
imap_close($imap);
}

You'll have to setup a cronjob (scheduled task on Windows) that connects to the Exchange or POP server, retrieve's all new emails sinds the last run and insert them into the DB.
In case of a POP mail server(I think POP will be easier), there is a comment here with all functions you need right -> here. If that doesn't work, try Googling for "PHP POP wrapper" or something similar.
In case of a Microsoft Exchange server, you'd have to use a PHP Exchange wrapper, found one here for you.
Goodluck!

You can pipe your email to a php script and then extract each part of email and store it in data base.
check this example
when you get content of email you can do what ever you want

Related

What does this email from SendGrid is refering to in PHP API?

a few days a go we received a email from sendgrid stating they are going to change some behaviors in their API, I've done research but I have not found concretly what they are refering to, currently we are using the PHP API, and atleast for the 2) I think we are OK as we use SendGrid classes to build the emails
Check your code that uses the mail send endpoint and ensure that the
“enable” parameter is included under the filter when applicable.
What do you need to do?
To avoid disruption to your mail send service, please be sure to make
the following actions before August 10, 2021
Check your code that uses the mail send endpoint and ensure that the
“enable” parameter is included under the filter when applicable.
Check your mail send header to ensure that you are using just one
X-SMTPAPI header and address header of the same kind. Remove multiple
headers of the same kind, so you have only 1 header of the same kind.
For example, if you are currently using multiple “from” headers, you
should modify your code so that you have a single “from” header.
Check your code to ensure that you are not applying any
personalization block substitution to your custom argument fields.
But what about the third item? we have substitions of following fashion: $mail->personalization[0]->addSubstitution('%url%', $link);
But I havent found anything like a "block sustitution in custom arguments"
And the first item also worries me, I haven't found anything like that neither, so I'm afraid that perhaps there is something that the PHP API does behind the scenes.
This a example of the code we use.
$sendgrid = new SendGrid(env('SENDGRID_APIKEY'));
$from_m = new SendGrid\Email(null, $from);
$to_m = new SendGrid\Email(null, $from);
$content = new SendGrid\Content("text/html", $body);
$mail = new SendGrid\Mail($from_m, '.', $to_m, $content);
$mail->personalization[0]->addBcc($tos);
$mail->personalization[0]->setSubject($subject);
$mail->personalization[0]->addSubstitution('%MemberName%', $name);
$mail->personalization[0]->addSubstitution('%url%', $hash_url);
$mail->setTemplateId($template_id);
$sendgrid->client->mail()->send()->post($mail);
We are using sendgrid/sendgrid: ~6.2
Twilio SendGrid developer evangelist here.
From my reading of your code and the guidelines you've been sent, you should be ok.
You don't appear to be using anything that needs to be enabled via the API, though you can check all the mail_settings and tracking_settings in the API reference here.
As you are using the library and calling the API you don't appear to be sending any extra headers.
You don't seem to be using any custom arguments, so you aren't applying any substitutions to them.
I do recommend that you upgrade your PHP helper library to version 7 from 6. That will ensure that the underlying library is also using the API in the most up to date manner.

How to detect drafted emails in IMAP. Using laravel-imap package

I am using Laravel-imap package (https://github.com/Webklex/laravel-imap) to fetch emails using imap.
I'm able to make a successful connection, able to fetch folders and emails, all set in that part.
However, I need to ignore drafted emails but I'm not able to detect that if a message is draft using code because email objects seem to have similar properties. I am testing using my Gmail account imap settings.
Here is the code I'm using. Any kind of help would be appreciated. Thanks
$oClient->connect();
$aFolder = $oClient->getFolders();
foreach($aFolder as $oFolder){
$aMessage = $oFolder->messages()->all()->get();
/** #var \Webklex\IMAP\Message $oMessage */
foreach($aMessage as $oMessage){
print_r($oMessage);
}
}

How to set Unique open tracking in sendgrid with php

The following code I am using to send mails from php.
<?php
require 'vendor/autoload.php';
$email->setFrom("test#example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test#example.com", "Example User");
$email->setOpenTracking(true, "--sub--");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$response = $sendgrid->send($email);
?>
Here I am setting Open Tracking for web-hook, When an email is opened the open event trigger and response coming to my given URL. Here I am getting web-hook every time when ever that email is opened by client. But I want Unique Open tracking.
I tried this which not working and may be invalid method:
$email->setUniqueOpenTracking(true, "--sub--");
Is there any setting or parameters That I need to configure.
Thanks in Advance
After call conversation with send grid, they are not providing Unique open tracking.
I have to maintain the opened web-hooks in a table and check every time, whether the web-hook is already received or not.

XMPPHP as live support chat

My idea is to integrate a live support chat on a website. The users text is send with xmpphp to my jabber client with the jabberbot sender id and if I answer, the jabber bot, takes my answer and transfers the text to the user.
There is only one problem. How do I separate different users or different chats? I don't want all users to see the answer, but the user who asks. Is there a kind of unique chat id or another possibility, that I might just missed?
User => Website => Chatbot => me
I want to answer and send it back to the user, but how can I find out the correct user from my answer?
Last time I have to solve this problem I used this architecture:
Entlarge image
The Webserver provides an JavaScript / jQuery or flash chat.
After chat is started, the client ask the server all 1 Second for new Messages.
Alternative for 1 Sec Polling
If that is to slow for you, have a look at websockets.
http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial
http://demo.cheyenne-server.org:8080/chat.html
But Websockets could no provided by php. There for you need to change php + apchache agaist node.js or java.
Plain HTTP PHP Methode
In PHP you will connect to the PsyBnc with is polling the messages from the supporter for you.
The PsyBnc is an IRC bot.
The reason why don't directly connect to XMPP or BitlBee is that those protocols don't like the flapping connect, disconnect from PHP. Because you can not keep the session alive, you need something that is made for often and short connects. This is the PsyBnc.
I would use something like this:
http://pear.php.net/package/Net_SmartIRC/download
<?php
session_start();
$message = $_GET['message'];
$client_name = $_GET['client_name'];
if (empty($_SESSION['chat_id'])) {
$_SESSION['chat_id'] = md5(time(). mt_rand(0, 999999));
}
if (empty($_SESSION['supporter'])) {
// how do you select the supporter?
// only choose a free?
// We send first message to all supporter and the first who grapped got the chat (where only 3 gues)
}
$irc_host = "127.0.0.1";
$irc_port = 6667; // Port of PsyBnc
$irc_password = "password_from_psy_bnc";
$irc_user = "username_from_psy_bnc";
include_once('Net/SmartIRC.php');
class message_reader
{
private $messages = array();
public function receive_messages(&$irc, &$data)
{
// result is send to #smartirc-test (we don't want to spam #test)
$this->messages[] = array(
'from' => $data->nick,
'message' => $data->message,
);
}
public function get_messages() {
return $this->messages;
}
}
$bot = &new message_reader();
$irc = &new Net_SmartIRC();
$irc->setDebug(SMARTIRC_DEBUG_ALL);
$irc->setUseSockets(TRUE);
$irc->registerActionhandler(SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, '^' . $_SESSION['chat_id'], $bot, 'receive_messages');
$irc->connect($irc_host, $irc_port);
$irc->login($_SESSION['chat_id'], $client_name, 0, $irc_user, $irc_password);
$irc->join(array('#bitlbee'));
$irc->listen();
$irc->disconnect();
// Send new Message to supporter
if (!empty($message)) {
$irc->message(SMARTIRC_TYPE_QUERY, $_SESSION['supporter'], $message);
}
echo json_encode(array('messages' => $bot->get_messages()));
Connect the support instant messanger to PHP
We have allready an IRC connection to the PsyBnc, now we need to send messages from IRC to ICQ, XMPP, GOOGLE TALK, MSN, YAHOO, AOI...
Here for is a nice solution named BitlBee.
BitlBee offers an IRC Server with can transfer message from and to nearly all instant messager protocols. By aliasing those accounts. For example you need for your system only 1 Server account at google talk, icq ... and at all your supporter to the buddylist of those accounts. Now BitleBee will provide your boddylist as an irc chat.
Your requirements are rather confusing. As Joshua said, you don't need a Jabber bot for this. All you need is a Jabber server - which you should already have. What you do is, you create a volatile user account sessionid#*yourdomain.com* whenever the chat feature is used and then you can just reply to any incoming message like normal while your website client can fetch the messages meant for it whenever.
Alternatively you could create one user account - qa#yourdomain.com - and use XMPP resource identifiers for the routing part. XMPP allows for something like qa#yourdomain.com/*sessionid* and you should be able to tell your XMPP library to only query a specific resource. Most XMPP client software will also reply to a specific resource by default and open a new conversation when applicable. This method is less "clean" than the first, but it would work somewhat better if you can't arbitrarily create user accounts for some reason.
I don't know what XMPP server you are using, but you could also try the Fastpath plugin and webchat for Openfire. Which is meant to provide a support team service over XMPP.
That being said, your question itself seems to imply nothing more than the standard chat feature of XMPP, which is between two users. It just means that the support person has a unique chat with each user asking a question. No other user will see that conversation.

Help with gmail message reading with php

I need to read emails from gmail but i cant connect to gmail pop3 server.
Can anyone help me here ?
Here the code:
$pop3 = new POP3;
$pop3->server = 'pop.gmail.com';
$pop3->user = 'username';
$pop3->passwd = 'password';
$pop3->debug = true;
$pop3->pop3_connect()
The result:
Warning: fsockopen() [function.fsockopen]: unable to connect to pop.gmail.com:110 (Connection timed out) in /public_html/cron/pop3.php on line 64
Thanks
According to this page (connecting to Gmail using Outlook Express), you have to use port 995 for POP3 access to Gmail, and furthermore, SSL must be enabled.
Wikipedia also states this:
E-mail clients can encrypt POP3 traffic using Transport Layer Security (TLS) or Secure Sockets Layer (SSL). A TLS/SSL connection is negotiated using the STLS command. Some clients and servers, like Google Gmail, instead use the deprecated alternate-port method, which uses TCP port 995 (POP3S).
I'm not sure if it will help you, but GMAIL has an ATOM feed. I wrote a PHP script to download the Atom Feed, using CURL, so that I could check my email on my antiquated cell phone that only supported very simple HTML. So, depending on what you want to do, it might be easier to download and process the ATOM feed than it is to connect to the POP server.
I don't know what class you're using -- but for instance, using Daniel Lemos' package is shown below. The key is choosing the right port (995), and the right encryption method (TLS set to true for whatever pop3 package you are using). For example, you could use something like the below to initiate the connection. Not a big fan of how this class is architected, or the sample code (lot of nested if statements), but it does the job.
$pop3=new pop3_class();
$apop=0;
$pop3->authentication_mechanism="USER";
$pop3->debug=0;
$pop3->html_debug=1;
$pop3->join_continuation_header_lines=1;
$pop3->hostname = "pop.gmail.com";
$pop3->port = 995; // The port that gmail uses...
$pop3->tls = 1; // This is encryption
$user = "someuser";
$password = "some password";
if( !empty($error=$pop3->Open()) ){
die( "Something terrible happened..." );
}
$pop3->Login($user,$password,$apop);
I think there are two easy options to your email:
Cron atom feed like Kibbee says. But then you will have a little delay between when message was sent and when you fetch it.
Use http://smtp2web.com/ which will post your email to your website which means a lot shorter delay. Offcourse privacy should not be crucial, because your mail will pass through intermediate.

Categories