PHP as a bridge to an smtp/pop3 server - php

Is it possible if I want to use PHP script to be used as the bridge or the middle man from the clients to the real smtp/pop3 server? The real server is behind a firewall and will not be configured to be accessible for the public. So I need a fake server which just relay the email to the real smtp server. Is it possible to be done?

I'm fairly sure it is fundamentally impossible to have a PHP script on shared hosting perform the functions of a true POP3 / SMTP proxy.
The requests coming from the clients would be using those protocols, and try to connect to your Godaddy server. That server will either have its own POP3 / SMTP server listening on those ports, or none.
You would have to use a server on which you have full root privileges to do this - but then, you will no longer need PHP, there will be better tools to do that available to you there.

Theoretically you could write a proxy, you'd have to implement the wire-level SMTP / POP3 / IMAP protocols in PHP. This would have to be run a a service (i.e. CLI, not inside Apache). If you need a mail proxy, have you considered nginx?

Related

PHP send email without internet connection via proxy server which have internet connection

I have PHP application which running without internet.I want to use this application to send email via local proxy server which have active internet connection.
This is a very broad question that's more suited to Server Fault than here. SMTP's store-and-forward architecture means that proxying email isn't especially easy, and for the most part it's reserved for load balancing across clusters of inbound servers in high-availability situations, rather than outbound which is considerably less reliable.
The way you would normally go about this is instead to use a relay rather than a proxy. You send to the relay, then the relay sends onwards to the internet. Install a mail server on your proxy server and configure it as a relay for your local server's mail server. There are many guides on how to do this, such as this one. Nearly all such guides assume that you are sending via some external service like gmail which requires authentication, so you'll see lots of references to sasl and the line, however, since it's on your local network you can instead whitelist its IP so you can relay without authentication. In postfix this is configured as per the docs.
When you've done that, test it by sending a message to yourself using a command line mail client from your server:
echo "test" | mail -s "Relay test" -a "From: you#example.com" recipient#example.com
You can trace the delivery of that message from your local server, through the relay, and on to your own mail server by reading the log files of each.
Once you know your infrastructure is working, you can use it from PHPMailer in two ways: talk directly to the relay server:
$mail->isSMTP();
$mail->Host = '10.0.0.2'; //Your relay's internal IP
$mail->SMTPAuth = false; //Don't need authentication if whitelisted
//Prevent PHPMailer trying to use encryption; it won't work with a literal IP
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = false;
Or use your server's local mail server to do the relaying for you:
$mail->isSMTP();
(default properties will work, so all you need to do is enable SMTP).

GAE Sending emails via SMTP

Reading up on GAE as a possible alternative to dedicated hosting (or VPS)...
Seems I would need to re-write a bit of code, if I had modules which relied on SMTP. Does this mean that even if I connected to a remote SMTP server that code would need to change to an HTTP API (assuming one is even provided by an third party SMTP provider such as smtp.com).
Yes - AppEngine/PHP does not yet support sockets and thus you can not connect to external SMTP servers. Also, even with sockets you would not be able to use SMPT on port 25, but authenticated SMTP on port 587 - see socket limitations.
OTOH, you can send email from AppEngine via the Mail API.
If you still want to send email via an external mail provider, you need to choose one that supports HTTP API, for example Amazon SES.

Getting server to send mail via SSH

I had written a program (In Android, client side and PHP, server side) to upload a file to a server over HTTP. Due to various reasons I had to change the system to do everything in SSH (for security among other things).
I have the basics set up (using jsch on Android). But I want to implement the equivalent of the PHP mail() function. When the file is uploaded, originally my PHP file automatically sends a mail from the server to a certain address.
I'm struggling to find a way to implement this within a shell on the server. So the question is, how do I automatically send an email from server in SSH?
EDIT:
Forgot to mention server is CentOS.
SSH itself has no mail function - it only supports shell access (which might include X and SSH agent forwarding), file transfer (or other subsystems that might be integrated into the server) and port forwarding.
So, you have basically these options:
Call some server-side shell command that causes the mail to be sent, as mentioned in the comment from Marc.
This would use a shell channel.
Use port forwarding to access an SMTP server on your server host (or any host that accepts mail from there).
If you want to send from the same program which uses JSch, there is no need to actually do client-side
port forwarding, instead simply use a direct-tcpip channel, and set its host and port properties
before connecting.
Then you'll have to implement the SMTP protocol yourself, or use any other library which supports SMTP. (I suppose JavaMail can do this, but I didn't explore how you can configure it to use JSch as a tunnel.)

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.

sendmail with a 'different' SMTP host solution : pro and cons

What are the pro and cons of using the SMTP host option of my choice?
Do sending mails from localhost require a specific SMTP host?
Is there any discernible difference on the receiving end between a mail sent via the server where the site resides and a dedicated mail server?
thanks
Luca
This depends if localhost refers to a public server with a proper DNS and reverse DNS entry, or if localhost is a machine you operate from a residential Internet Service Provider.
If localhost is a real server, outbound mails may be delivered a little faster if sent from the same server, and configuration in PHP & others is generally a lot simpler to setup.
If it isn't a proper server though, especially having proper DNS entries, the likelihood of much mail from it actually reaching recipients is low. Most receiving SMTP servers will reject mail if they cannot validate the source via a reverse DNS entry.
If you use a dedicated mail server, you get the benefit of all the inbound & outbound logging being on the same machine, and when receiving SMTP hosts lookup your domain's MX record in DNS, it will already be configured as the mail server rather than the web server. But this just requires a little more configuration on the web server, especially if the mail server requires authentication from the web server rather than treating it as a trusted relay sender.

Categories