当前位置: 动力学知识库 > 问答 > 编程问答 >

php - site on server A, want to send its email from server B

问题描述:

I have a rather messy situation that I can't seem to think of a way to resolve.

1 - I am maintaining a live site on a shared host, the hosting company has a limit of 250 emails/hour (or 250 smtp relays per hour). The site is a dating site so generates a lot of email, so the limit is being reached pretty regularly.

2 - I have a VPS from which I can sent unlimited emails.

I want to use 2 to send emails for 1. The obvious answer is to move the site to server 2, but this is not an option currently.

Both setups are php/mysql.

thanks in advance.

网友答案:

Security considerations

  • Host 2 should have SSL(Because you have VPS this could and maybe already enabled) . Otherwise other user from the shared hosting could sniff packets because it will be sent in plain text if host 2. You should keep a close look at your mail logs.

Solution A

  1. When you want to sent an email from 1 to 2 make an asynchronous request(if needed) from 1 to 2. When you do request asynchronously the users does not have to wait for email to be sent. You could for example use this snippet to achieve that. url 2 should be an url that's hard to guess as some sort of security measurement(basic). For example https://www.myvps.com/gfgfdgfdgfcascxzsdadf3rfdfvs3fd (just some random string).

  2. From url 2just get the post fields and sent e-mail. I guess this is pretty simple solution to implement.

advantages:

  • simple to implement

disadvantages:

  • Could have scaling issues. Each time url 2 gets called a new process is going to be spawned and there is no way to throttle sending e-mails. I don't think for your use case you will hit this wall.
  • if host 2 does not have SSL your system could be unsafe.

Solution B:

You could use Google App Engine(GAE) for sending your emails. The first 1000 mails are free and after that it will cost you $0.0001 per Recipients emailed.

advantages:

  • Easy to implement(Python is pretty nifty language). There is also no need for asynchronous request because GAE's mail is asynchronous.
  • GAE has SSL support so the connection from server 1 can not be sniffed.
  • It scales because it is on GAE.

disadvantage:

  • Sending email will cost you money. Maybe you could drop your VPS and go GAE instead(save money?).

SOLUTION C:

This solution requires you to install node.js enviroment(but because you are on a VPS this should not be a problem). node.js requires python(>=2.4) to install node.js.

advantages:

  • Insanely fast. I don't even think you need throttling.
  • Easy to implement(node.js/javascript is a really nice). Again no need for asynchronous request because node.js is asynchronous.
  • Scales(easily) because you can do throttling with almost no effort.

disadvantages:

  • Has dependencies(node.js). And some node packages(express, connect, node-email)
  • could be unsafe if not having SSL.

Code

I wrote an email service in node in just a couple of minutes.

const PORT = 4000;
const HOST = 'localhost';

const lib = require('email');
lib.from = ''; // #Should set this to e-mail you want to sent from.
const Email = lib.Email;
const express = require('express');

var app = module.exports = express.createServer(
      express.bodyDecoder()
);

app.post('/secret', function(req, res, params) {
    var to = req.body.to;
    var subject = req.body.subject;
    var body = req.body.body;

    if (!(to && subject && body)) {
        return res.send('param missing');
    }

    res.send('+ok');

    var mail = new Email({
        to: to,
        subject: subject, 
        body: body
    });

    mail.send(function(err){
        if (err) console.log(err);
    });
});

if (!module.parent) {
    app.listen(PORT, HOST);
    console.log("Express server listening on port %d", app.address().port)
}
网友答案:

are you able to configure server 2 at all? if so, can you set it to relay messages for server 1. If you can, then you just point server1 php script to use host2 rather than localhost for SMTP send.

if you cant configure as described above, you could build a page on server2 that server1 posts the email to be sent to, then send from server2... Sooo, on server2 you would have "relay.php" which takes in posted values representing the email to be sent (addressFrom,addressTo,subject,body). You would use curl on server1 to issue a post request to relay.php. Its ugly, and may not perform too well, and not as good as using SMTP, but it will do what you want. ideally, look into getting server2 to accept SMTP relay from server1 - your host can help you out there maybe.

网友答案:

Set up a PHP script on the VPS, using a hidden url, that convert it's POST requests into emails.

That's not the most secure option (actually, not secure at all). If you care about security (and you should), try setting a ssh tunnel between the two and transfer the messages through that.

分享给朋友:
您可能感兴趣的文章:
随机阅读: