Are there any SMTP servers written in PHP or Go? - php

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.

Related

Using PHP to send mails from my organisation's email id?

So, I developed this web application which needs me to send emails to everyone (details in a mysql database), providing a unique set of user credentials. I have a virtual server on the University network where I am hosting my PHP based application, so it obviously doesn't have its own mail server/SMTP settings.
So, if I were to send out mails from a university network, I guess I should request the network admin for the SMTP settings for the university mail network and then use PEAR for it right?
I need to confirm this because it's kind of a big deal to make a request like that, and I myself have never used PHP to send mails 'from' an external email id not linked to the server.
Are there any alternatives to this?
Yes you can use PEAR::mail to send mails via SMTP server. However, if SMTP is configured on localhost you can simply use mail() function to send mail. You can also connect to SMTP server via socket but then you need to handle whole protocol communication.

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.

MTA for receiving mails, in 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.

Is it possible to send an email from PHP without using the SMTP server

I need to send a newsletter to several thousands of subscribers with PHP.
The hosting service I am using allows me to send 300 mails/hour tops with their SMTP server.
They told me that if I send email with PHP without authenticating or using the SMTP server I won't have any problems with limits.
Is that even possible? Doesn't the mail() function in PHP use SMTP to send mail?
The mail() function will use whatever php.ini tells it to use which may be sendmail or may be an external SMTP server.
You have a few different options:
If they're not time sensitive, use their SMTP server and throttle yourself;
Alternatively, if they are time sensitive, it may make sense to authenticate against your own external SMTP server;
Finally, I'd suggest looking at a system like MailChimp or iContact. They'll let you send to anyone on your list and will handle bounces and unsubscribes for you. Even better, their servers have been whitelisted by ISPs, etc, so you're much less likely to have your messages flagged as spam.
My 0.02
On unix/linux, mail() is almost always configured to just use the local sendmail facility.
Technically speaking, you're still using SMTP servers, but not at your ISP. Sendmail communicates directly with the SMTP server responsible for incoming mail for each recipient.
While it's possible that your host has sendmail to route all mail through their SMTP server, it's unlikely.
I'd say just use plain old mail() and give it a shot.
The hosting company probably provides you with a SMTP server you can use, and it is that server that probably has the limitation. You can avoid the limitation by using another SMTP server (one that they aren't providing.)
All e-mail is traditionally "sent" using SMTP. You would need to configure your machine to use an external server.
http://email.about.com/od/emailprogrammingtips/qt/Configure_PHP_to_Use_a_Remote_SMTP_Server_for_Sending_Mail.htm
For a good general discussion of successfully sending e-mails from code, see this Coding Horror post. I noticed one of the comments mentioned the Postmark app as a paid alternative to using your ISP's SMTP server. I've never used it, so I don't know if it's worth the price.

swiftmailer email messages don't arrive

Hello I am trying to send email messages with swiftmailer version 4.0.3.
I get a returncode 2. And it seems the messages are sent, however they don't arrive.
I am using the sendmail transport mode
$this->psTransport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -t');
I get a return code of 2 and no exception happens.
Anyone familiar with this problem?
The SMTP Transport, Swift_SmtpTransport is without doubt the most commonly used Transport because it will work on 99% of web servers.
It is a more profissional way to do what you want. See the docs:
http://swiftmailer.org/docs/smtp-transport
SMTP Transport is simple and your messages are better recognized by the receivers because it's not your webserver sending your emails, but an email server already stabilished, with a known and approved ip.
Using PHP sockets to send mail is most of the times penalized by remote SMTP servers and detected as spam. I've suffered the same issue several times.
My advise is to use a SMTP transport method instead and send your messages through a recognized and validated SMTP sender server. If you are not sending too much messages per hour you can even use GMail servers, I use them very often for tests and small applications.

Categories