I am about to start a project that requires an email to be somehow ran through a PHP script. I have full control of the server that the email will be sent to and wondered if people could give me some thoughts or pointers as to the most elegant way parsing it with PHP. I am not editing the email and then forwarding it on.
The server is CentOS with Exim email.
Thanks in advance.
You implement a client for the mail box (php imap modules can work with pop3, imap4 and local mailboxes). You poll the mailbox for new arrived messages, retrieve new ones and parse it. All using the php imap module.
One of the best examples of this comes with Wordpress... there is a file wp-mail.php which is set up as a cron task to retrieve and parse emails... I have hacked it up several times to do such things!
To make life easier for you, rather that stress yourself with parsing emails (if you don't mind the cost) you could decide to use Postmark
I've been using their services for quite some time and i love them. They have an Incoming email API service now. Enough talk, simply check it out because I believe it will help with what you're trying to do.
There are three main approaches to this:
Run a cron task to poll an IMAP/Pop3 server every x minutes
Make exim run a script whenever it receives an email
Use a third party service to receive the email and send it on to your site.
I wrote a Blog Post detailing the options, although it's for Rails the main concept applies to any language including PHP.
Related
I have special e-mail accounts on my web server (#mydomain.com). I want to run a php script automatically when one of these accounts get a new e-mail. For example: when info#mydomain.com address receives a mail, I want to run "receivedMail.php" file and read this new e-mail. I don't know where I will start or how can I do this.
You're wanting to pipe email received for a specific email address to a PHP script.
If your webhost has cPanel, this makes it very easy to setup. See:
http://kb.siteground.com/how_to_pipe_an_email_to_a_php_script/
Alternatively, if you don't have cPanel but have Exim mailserver, this will guide you on how to do this:
http://www.phpshare.org/articles/Piping-Incoming-Mail-with-PHP
Hope this helps!
You have to create a cron job calling your php file every 1 minute for exemple.
The script have to the derver and read the email.
This has nothing to do with web servers directly, Since web servers do not receive emails. So no .htaccess style files come into play. The email is received by a mail server, so that is where you have to get active. Two strategies are possible:
you use the possibilities to trigger an action as offered by your
mail server (typically the smtp server you operate). That obviously
depends on which mail server you operate, different software offers
different features.
you poll those email accounts on a regular base, using a standard protocol like POP3 or IMAP4. You can do this using any suitable client. When a new message is found whilst polling the account, then you trigger the action you wish.
Option 2. is probably easier to start with. So give it a try: create a php file which polls your email server. You can use the imap php extion for this, it supports all important email protocols. The extensions allows to easily detect and retrieve new messages. For each such message you can implement whatever action you wish. All that is left is to run this script on a regular base, say every 3 minutes, which is typically done using a cron system. There are many examples for this out there on google...
Have fun!
I have web application, and I want to implementation email system. My web application is a social network: users receive email when users update status and any another user is commented to their status.
What is the best way to implement that? I have been implement email system after users is comment, then in the same time I'm sending email to their user. The second option to use a schedule to send email.
I am using PHP and SwiftMailer.
To clarify: should I directly sent email to users, or should I use a database to queue mail and send via cron?
Please try phpmailer library functions
You could also try PostageApp.
http://postageapp.com/
You can use inbuilt php function .Please visit the following link.
http://php.net/manual/en/function.mail.php
My advice is to use a mailing service with an API, such as Sendgrid for large amounts of mails (>100 per day), and otherwise just use your php mailer of choice.
Sending mail via your own server has a couple of disadvantages, mainly related to getting blacklisted or marked as spam. This is because the other mail servers don't really know yours, and if suddenly a lot of mails start appearing, you look like a spambot.
To avoid sending a lot of mails at once, you also have to somehow queue the sending via batches, and for that you need cron jobs and such. Getting un-blacklisted is also not very easy, and simple domain changes will probably get you re-identified
Mailing services do relieve you of most of these problems, and are overall reasonably cheap. Learning the pitfalls of in-house mail solutions is usually too much for a single project, since it is a reasonably large topic.
Disclaimer: I do not work for any mail-sending service, I just happen to work in a place that had their domain blacklisted recently because of bad emailing practices.
I have developed a CRM that is used with a marketing website similar to manta.com. The CRM has a built-in email client. Basically it matches names and emails to addresses on file, and if the email is from a customer it loads their account information next to the email.
The entire system works, but is in some cases slow. Because each "region" of the country is managed by a different company, the software connects to several different mail servers, all of which behave differently.
In some cases (mostly with godaddy servers) it takes a very long time to send an email. It also takes a long time to load the inbox if they have a lot of messages (500+), even if it is paged.
I am using pear Mail::Factory smtp to send, and the built in php imap functions to receive.
I am thinking for outgoing messages I will just store the emails into a db and then have a cron script send it out, then they can send their email and move on right away.
Does anyone have any ideas/concepts for handling large inboxes via IMAP and php? I will have no control over the mail servers themselves, but have full control over the server that the CRM is on.
I am not looking for code, just concepts if you have them.
It is Linux (ubuntu), apache, mysql, php. I can use Zend if needed.
It looks like a combination of caching the emails and using the imap_fetch_overview function which omits the body of the email. This seems to eliminate any issues with the lag. I can also use it to page the results so I am only loading a small number (25-50) at a time. Thanks for the help.
I need in some way to accept incoming emails to my web application and save them in the database. My web application is written in PHP language and im using MySQL database.
I have no idea how can i do that... I builded the rest of my web application by myself but this is very complicated for me.
For example i need to know how can i accept incoming emails first... and where? in which email address my web application will accept these emails...???
Then how i will get them from there? How i will process them and how i will store them in my database?
Please help me with an example, some piece of code, anything!
Any help is accepted.
Im not an expert so a piece of code will help me a lot.
Thank you in advance!
You basically have two choices:
Run an SMTP program (such as Postfix or Sendmail) on your server to receive the emails, and use procmail to pass them to your code
get an email account on an external host service and retrieve the email using one of the POP3 client packages for PHP, such as pop3class
Option 1 is much harder to do, and you have to learn about administering an email server, which is not a trivial task. Option 2 is relatively easy and is the one I'd recommend.
Easiest way would be to setup a regular email address with some freemailer and then check this account.
A start for this might be the POP3-class of PHPMailer.
Ok..... basically what I'm trying to do is setup something that will handle all incoming mail on a certain POP3 email address, pass all the relevant data to a PHP script, which will then parse the data & insert it into a MySQL database.
Now I have been looking around a bit..... checked out procmail, but that just confused the hell outta me (I'm not much of a server guy, just know enough CL stuff to get by), so was wondering if there were any better options available to me or any existing PHP classes that made it a bit easier?
Thanks a lot - any help would be much appreciated!
Sounds like you have two tasks before you:
Write a PHP script that checks the POP3 account, processing any mail it finds.--You could use PHP's IMAP functionality, which supports IMAP, POP3 and NNTP.
Somehow get the above-mentioned script to run when new mail is received.--An easy way to simulate this effect would be to use a cron task to run your script regularly...perhaps every few minutes. Hopefully, your web host supports cron tasks--you may find an option to set them up in your hosting account's control panel or you may need to contact support for assistance.
Hope this helps,
Ben
There are some classes built to help you out as well:
http://www.phpclasses.org/package/2-PHP-Access-to-e-mail-mailboxes-using-the-POP3-protocol.html
As weird as it may sound, check out the imap extension of PHP. It supports POP3 and is fairly easy to use.