Getting server to send mail via SSH - php

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.)

Related

Create telnet client that uses server's TCP/IP stack instead of users?

If I used something server side like PHP inconjuction with some web wizardry for the interface, would it be possible to create a web-based telnet client that uses the server's TCP/IP stack instead of the users?
I've seen a PHP based MUD client, but it uses Websockets or Flash based on the user's machine. I'm looking to see if it would be possible to make use of the server's connection to generate a telnet session.
At work, telnet is blocked outbound. I would login to a web server that had this little web app running, and through that web server, I could utilize it's ability to telnet to another server?
Yes, but if it would be using HTTP, which is stateless, you would have a lot of overhead (like logging in each request).
Since it is telnet though, its fairly simple. Check out fsockopen. There's a few telnet examples in the comment section.

PHP as a bridge to an smtp/pop3 server

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?

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.

Exchange rejecting php mail from same domain

I have an IIS server running PHP on an Apache Module. I am running a domain on it, and this domain has a seperate setup for email which uses an exchange server. When I try to send email from the website using php mail() the exchange server rejects it. I.e. the header from line is sender#this-domain.com and it is sending to receiver#this-domain.com, both are email addresses set up in the exchange, even though the sender has no direct relationship with exchange.
The emails are not getting through. We tried using a Yahoo adress in the from line but nothing. Has anyone ever come across a problem like this.
You have to either configure the exchange server to accept relay requests based on the origin ip or some transparent authorization mechanism (don't know much about IIS and Exchange and integrated windows authentication, but the good people over at http://serverfault.com do. ). Or use some mailing library that can handle smpt authentication like e.g. http://swiftmailer.org
I once ran into a problem like this which involved the Exchange server not wanting to accept anonymous connections or that it would only accept mail from certain SMTP servers. Have you checked the configuration on your Exchange server to eliminate those possibilities?

PHP Send email over SSH

We have a website that emails the contents of a contact form to our client. We would like to change this to make it a bit more secure (sensitive data can be sent over the contact form), we have encrypted the page with an SSL certificate so the only weak link we have at the moment is the email.
We already have an SSH connection setup to our client's servers and I was wondering if anyone knows if it is possible to send an email (possibly with attachments) over that connection?
Our web server is a linux machine and we are able to install applications ourselves. The client's server is a unix server, however their email system is Microsoft Exchange.
We've discussed PGP and it is not an option for our client, they will not install it on their systems.
Thanks for any help.
Peter
To make my answer clearer I see three (3) systems here.
The webserver running the PHP script; later refered as the WebServer
The Unix server running on your client site; later refered to as the UnixServer
The Exchange server running on your client site; later refered to as the ExchangeServer
What you could do is use autossh to tunnel a connection from the WebServer through the UnixServer server with an endpoint at ExchangeServer. It is no end-to-end encrypton since you would not have an encrypted link from the UnixServer to the ExchangeServer it gets you closer to your goal.
Full documentation on autossh can be found here
The basic steps for the setup would be this:
A. Setup passwordless authentication from the WebServer to the UnixServer. You'll have to be carefull to protect the RSA key generated otherwise you've just opened a gaping hole on their system. Ideally run autossh under an unprivilege user and have the account used on the UnixServer to be unprivileged as well. Make sure you test the connection before you start using autossh
B. Setup your ssh options for keepalive (autossh is a fallback option). Make sure that the ~/.ssh/config file for the user launching autossh has these option setup:
TCPKeepAlive yes
ClientAliveInterval 60
ServerAliveInterval 15
ServerAliveCountMax 3
C. Setup autossh. Linux.com as an articile about it here and debianadmin.com as one here.
If the client's server is configured so the "mail" command works you ought to be able to use that. And it is easy to test: ssh in and try it.
Otherwise, you probably want to go with smtp over SSL with encryption direct to their mail server.
I'd be inclined to store the information securely on the server, and email a notification to the user who then can login and see this request (and all other recent ones?) in the web page.
You don't have to mess around with trying to make email secure and working with systems that aren't in your domain of expertise, you can secure your PHP app properly, and take advantage of security features already easily available in browsers.
ssh -L $LOCAL_LISTEN_PORT:$EXCHANGE_SERVER_IP:25 username#sshdserver
http://www.ssh.com/support/documentation/online/ssh/adminguide/32/Port_Forwarding.html

Categories