让服务器通过 SSH 发送邮件
我编写了一个程序(在 Android 中,客户端和 PHP,服务器端)通过 HTTP 将文件上传到服务器。由于各种原因,我不得不更改系统以通过 SSH 完成所有操作(出于安全等原因)。
我已经设置了基础知识(在 Android 上使用 jsch)。但我想实现与 PHP mail() 函数等效的功能。当文件上传时,原来我的PHP文件会自动从服务器发送邮件到某个地址。
我正在努力寻找一种在服务器上的 shell 中实现此功能的方法。那么问题来了,如何通过 SSH 自动从服务器发送电子邮件?
编辑: 忘了说服务器是CentOS。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SSH 本身没有邮件功能 - 它只支持 shell 访问(可能包括 X 和 SSH 代理转发)、文件传输(或可能集成到服务器中的其他子系统)和端口转发。
因此,您基本上有以下选项:
调用一些服务器端 shell 命令来发送邮件,如 Marc 的评论中所述。
这将使用
shell
通道。使用端口转发访问服务器主机(或从那里接受邮件的任何主机)上的 SMTP 服务器。
如果您想从使用 JSch 的同一程序发送,则无需实际执行客户端操作
端口转发,只需使用
direct-tcpip
通道,并设置其主机和端口属性连接之前。
然后您必须自己实现 SMTP 协议,或使用任何其他支持 SMTP 的库。 (我想 JavaMail 可以做到这一点,但我没有探讨如何将其配置为使用 JSch 作为隧道。)
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 propertiesbefore 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.)