classicjonesynz
classicjonesynz

Reputation: 4042

Given two seperate servers; can PHP decide on the server to recieve e-mails?

I've been thinking, and I have question regarding multiple e-mail addresses and the mail function.

Is it possible to have PHP send an email to a particular smtp server; for instance if I have two addresses.

Configuration

smtp.fakecompany1.co.nz

Intended Recipient

Currently, if the smtp of the intended recipient (smtp.fakecompany1.co.nz) is setup then that recipient from the intended smtp (smtp.fakecompany1.co.nz) will receive e-mail; but my question is in regards to whether it could be possible to select an smtp (smtp.fakecompany1.co.nz) and send an email, without requiring to authenticate into a DIFFERENT smtp (smtp.fakecompany2.com).

email diagram
(source: iforce.co.nz)

TL;DR can I send an email from smtp.fakecompany1.co.nz to smtp.fakecompany2.com (given the email exists on both servers), without modifying the authentication details on the primary server (Due to technical issues with Gmail).

Upvotes: 1

Views: 53

Answers (1)

Joel Peltonen
Joel Peltonen

Reputation: 13432

You can SMTP with PEAR::Mail instead of using mail(). See this question for an example: how to use php pear mail

Here is a modified example I copied from the linked Q:

require_once "Mail.php";
$from = "<[email protected]>";
$to = "<[email protected]>";
$subject = "Hi!";
$body = "Hello world";

$host = "smtp.fakecompany2.com";
$port = "465";
$username = "<[email protected]>";
$password = "testtest";

if (YOUR HEADER CHECK HERE) {
    $host = "smtp.fakecompany1.co.nz";
}

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}

Upvotes: 1

Related Questions