MTA for receiving mails, in PHP - php

is there a library/class/code-snippet/etc. that allows me to directly receive mail in php?
So that I don't have to run an additional sever in an other process and then have to somehow send the mails to the already-running php-process.
I've been looking around for a while, but results for "php" and "mail" or "mta"/"smtp" on google focus mostly on sending mail, or retrieving it using pop3 or imap...
[EDIT]
What I'm trying to do is forward the messages to an IRC-channel, so obviously when the IRC-bot (in PHP) isn't running, loosing the mails is not a big deal. However having a low latency between receiving the mail and posting it on IRC is.

I've never seen a compete SMTP server in PHP and it doesn't surprise me. I don't think you want to go that route. I can think of two other ways to do this:
Use procmail (or similar) with your existing SMTP server and make a rule that forwards the messages to your PHP script. Pretty simple to do and it will fire the script the instant the message is received.
Have the messages delivered to some existing mailbox, then have your PHP script continually poll it (via POP or IMAP) for new messages. When you see a new message, pass it to IRC and delete it. How long it takes the message to appear depends on how often you poll the inbox.

Writing your own SMTP server to act as a MTA is a big undertaking. You could take a look at http://cloudmailin.com. CloudMailin allows you to receive the incoming email as an HTTP Post and acts as the MTA sending the email direct to your PHP app. The PHP app can then process the email and send it to the IRC channel.

The MTA (Mail Transport Agent) is an application (i.e. sendmail, exim) that is used to move mail from location to location. As far as I know, there is no MTA coded in PHP. PHP offers classes and scripting that will handle mail transport, but it still processes through an existing MTA.
You should be able to configure the MTA to pass mail through a given PHP script to accomplish what you are looking for.

Writing your own SMTP server is a huge undertaking. Do NOT go this route. You'll waste an incredible amount of time duplicating work that's been done already. Choose one of the 'big' SMTP servers (postfix, exim, sendmail, etc...) and go with that.
Don't think that just setting up a dinky little script to listen to port 25 will do the trick. SMTP servers are incredibly complicated beasts and the mechanics of setting up that port 25 socket likely occupy less than 0.00000000000000000000000000000000001% of the work. (this number is totally true, I asked my gut what it feels and that's what came out).

Try this: http://www.php.net/manual/en/refs.remote.mail.php
10 seconds of googling. SMTP is for mail relay, although it is the defacto protocol for mail clients to send mail, due to the Unix heritage of every box running an SMTP mail relay.
POP3 and IMAP provide mail clients' access to mail.

Related

Are there any SMTP servers written in PHP or Go?

I don't mean SMTP/IMAP clients, I mean a working SMTP server that can both receive and send email.
There are lots of examples of partial PHP and Go SMTP servers that only listen for SMTP connections - aren't there any which show how to send mail?
I'm really interested in learning this second half of the Simple Mail Transfer Protocol. I understand that there are a lot of bad servers which vary from the standard - but I want to learn how the whole protocol from start to finish is implemented.
I think you misunderstand how SMTP is supposed to work. Here is a simplified version:
The Mail User Agent (MUA) queues a message for delivery by sending it to the Mail Submission Agent (MSA).
MSA connects to the Mail Transfer Agent (the "smtp server") over SMTP.
The MTA then uses DNS to lookup the MX record for the recipient's domain. It then contacts the recipient's MX server as a SMTP client.
The MX server accepts the envelope; it then forwards it to a Mail Delivery Agent (MDA).
MDA then puts the envelope in some message store where some IMAP or POP3 server reads messages. The MUA then connects to these servers to retrieve the message.
The entire process uses three main commands. MAIL, RCPT and DATA.
MAIL = Envelope information, bounce addresses, etc.
RCTP = The recipient.
DATA = The payload.
The SMTP server responds - much like HTTP actually, with error codes and based on that, the MTA knows what to do with the envelope (its a bounce back, so send appropriate reply, etc.)
In this process there is no such thing as "retrieve email" (ignoring ETRN for a bit); as SMTP is purely for email transmission and not retrieval.
I found a full SMTP server written in PHP - even includes a nasty open relay.
$ sudo php php-smtp.php [ip-address] [port]
There is no "second half" of SMTP, just the protocol. If your MUA interacts directly over TCP with the mail server (rather than using a helper program like the /usr/bin/sendmail binary found on most Unixes), then it uses the SMTP protocol. The MTA uses the same protocol to talk to other MTAs when delivering the mail. It may use a larger set of the available verbs, depending on the circumstances.
Seeing the code of a PHP or Go implementation of an MTA would show you how one person/team has implemented the SMTP protocol.
THere's a PHP smtpd server that just processes mail - https://github.com/flashmob/Guerrilla-SMTPd
and a port to go
https://github.com/flashmob/go-guerrilla
There is a non blocking SMTP server writen in PHP on top of ReactPhp:
https://bitbucket.org/david_garcia_garcia/smtpserver
It is designed for the end user to have custom Authentication and Delivery implementations, the rest of the SMTP behaviour works out of the box.

PHP mail() not working

I'm building a site on my home computer using MAMP. The code I'm using employs the PHP mail() function to send emails, but whenever I test it, the mails aren't getting sent.
My computer is connected to the net, but I'm wondering if there's something about local hosting that prevents mails from getting sent. I'm not getting any kind of error message.
Any ideas?
PHP can send mail in one of two ways.
The first, and the default on non-Windows systems, is to use the local mail transfer agent installed on the system. This would be "sendmail" or an application compatible with it, the most popular probably being postfix.
The other is to connect via SMTP to some mail server.
You will either need to install a mail transfer agent on your local system (and set it up correctly), or edit PHP's configuration to specify an SMTP server address and port.
Yes, there are things that could block locally hosted mail. For one, your ISP could block SMTP to servers other than the ISP. ask your ISP support if they block SMTP... Or try telexing so someone's MX port 25 and do you get a response?
If your ISP blocks smtp you can still send the mail, but first you must relay that email through a hosted email server like your ISP mail server. This process is called 'smart hosting' and you can search for more info.
Even if you are not blocked on port 25, many sites will refuse or lose smtp traffic that originates from a dynamic or residential IP address, so again the smart host suggestion.
Also I suggest not using the built in mail() function in PHP... Use something that replaces and improves it like http://pear.php.net/package/Mail or http://sourceforge.net/projects/phpmailer/. Again, use the SMTP method as it is way more reliable than direct sending or calling Sendmail.
It is important to confirm this problem, doing SMTP manually over telnet. That way you isolate the problem from PHP. I did ISP support for years and saw this question lots. Most people setup php and mail correctly but get stuck on a background network issue with SMTP.
If you have Wireshark installed, it can record network traffic and you might see the actual SMTP traffic, for example the remote server may be refusing your connection. Wireshark is helpful but not required to solve this though. Good luck.
You need to setup SMTP server in order to be able use mail function, or you can use PHPMailer class, with it you can avoid using mail function and setup problems, PHPMailler need socket extension to be loaded in order to function correctly.

Reduce mail sending time

I am sending bulk mail using phplist third party component. But when I am sending above 1000 mail it's taking too much time to send that message. It's taking 4 to 5 hours to send all the mails completely.
Is it possible to reduce the mail sending time? How?
The easiest way is to configure a local smtp server, and have that relay to the "real" smtp server. It shouldn't be taking that long to complete, from code, the email sending. But the time of "actual" delivery is up to the nodes inbetween, and nothing to do with you.
As the previous poster said, configure a local queueing smtp server, if you have shell access to the server. Postfix is a good candidate, it's lightweight and easy to configure. Sending a thousand emails should not take more than a few seconds.
You could use the pear package Mail::Factory as well to specify a lot of options and use various methods of calling the MTA (Mail Transport Agent, i.e. Postfix).

Is there a SMTP mail transfer library in PHP

I want to write an email transfer service and need a MTU replacement of sendmail/postfix.
I'm not looking how to deliver to a transmitting SMTP server (like a postfix listing on a SMTP port). I also don't need the receiving part of the server, bounces etc. would go to a different existing postfix.
All of it in pure PHP. SMTP is a pretty easy protocol but this would require access to the MX DNS record and other lot of details that need to be taken care of.
Why do i need this? Because most shared internet providers have insane low limits for sending out emails like 500 per day. Thats almost nothing if you want to setup even the lowest traffic email list.
EDIT: Please Note: The code needs to connect to the receivers SMTP server and deliver the message with an adapted header set (removed BCC list, added Path route). If you see a SMTP class less then 5000 lines or requires you to configure a SMTP hostip and port then this is not the stuff i'm looking for.
It needs to do all the stuff sendmail is doing just as a PHP library.
I use Pear's Mail class.
EDIT
After re-reading this, I see there's more to it than just a library. You are asking to send direct to the remote MX.
Why reinvent the wheel? Set up an instance of postfix on the server, which only listens to connections from the web server... and let an MTA do what it does best. Hand off the messages from php to a real mail server and move on.
In the case of ISP's that block outbound port 25 and force the use of a smarthost, this also allows you to restrict the rate of messages sent to the smarthost.
Lastly, sending straight to the end MX from your php script is a bad idea, because if you send to me, I'll never get it. I and many other sites use "greylisting" to reduce spam, which denies all initial requests with a 450 temporary error. Real MTA's will try again, but unless you implemented a delay queue and try again, your message will never go through.
We use http://sourceforge.net/projects/phpmailer/ to do SMTP email from PHP
SwiftMailer is the only library you'll need.
Try Zend Framework component Zend_Mail (you can use the component independently of the entire framework).
Here is something I wrote. It's quite minimal and I don't know how well it performs, but I wrote it with the intention of replacing sendmail, meaning that it will take a message, lookup all the MX records for the recipient domains, contact those mail servers and deliver a message for the corresponding recipients. It worked well enough for me at the time.
https://github.com/denvertimothy/ThriveSMTP
It's been a long time since I've used it, but I threw it up on Github just now.
I used http://www.mailerq.com/ which works cool. Its a queue based mail transfer agent. It requires rabbitmq.
It provides multiple worker as well.easy to store in database.
It provided management console as well.
worth to check it

Has Anyone Here ever tried PEAR

Hi I have been reading alot of articles adoring the PEAR mail package and it seems like PEAR is something I need to try out.
I am interested in setting up a full mail server, similar to a conventional SMTP mail service; which incorporates mail queuing, resending with a backend database etc. My impression is that PEAR can do this but can its service be used with mail clients like outlook to send mail, just as how any any smtp server daemon can where one would enter a portnumber, server name and/or security protocol?
Thanks
No, PEAR isn't going to magically solve these problems for you.
PEAR is a collection of PHP classes that are meant to solve common problems faced by PHP users. The Mail packages offer code for interacting with different parts Email systems. They do not contain code for creating email systems from scratch.
For example, form the Mail_Queue documentation
The Mail_Queue class puts mails in a temporary container, waiting to be fed to the MTA (Mail >Transport Agent), and sends them later (e.g. a certain amount of mails every few minutes) by >crontab or in other way.
The MTA in this case in sendmail, postfix, etc.
Another example, from the Mail_Mbox documentation
It can split messages inside a Mbox, return the number of messages, return,
update or remove an specific message or add a message on the Mbox
Incorrect use of "an" aside, you are using this to read existing MBOX files, and not caring how they got there.
The Mail package is about interacting with existing mail systems, NOT creating replacements. You'll still need to understand how all those email systems work to create a "full mail server, similar to a conventional SMTP mail service". If you're doing this because you want to learn how email systems work, have at it. If you're doing this because you thing this will give your business some leg up in the email game, I laugh and say "good luck with that"
PEAR is a repository for lots of libraries. Some of them deal with mailing.
PEAR's Mail class is designed for sending mail only. It is not designed as an implementation of an SMTP server.
Pear Mail is an SMTP sender aka client, not an SMTP server. Although it's entirely possible to write server (any kind of server) in php that doesn't mean that writing an SMTP server yourself is necessarily a good idea as it requires quite some expertise to do it right (spam anyone?). If you want to see an SMTP server implemented in a scripting language, go have a look at Lamson, written in Python by Zed Shaw.
And while you're there, do read the About page. This quote says it all
However, as great as Lamson is for
processing email intelligently, it
isn’t the best solution for delivering
mail. There is 30+ years of SMTP lore
and myth stored in the code of mail
servers such as Postfix and Exim that
would take years to replicate and make
efficient. Being a practical project,
Lamson defers to much more capable
SMTP servers for the grunt work of
getting the mail to the final
recipient.
It seems to me that PEAR's MailQueue package may address your needs.

Categories