Travis
Travis

Reputation: 2018

Email and Web split on different servers: Send email via PHP to an email on that domain

1 Domain that has port80 and MX split on the DNS:

When I call the mail() function in PHP on the website that is addressed to an email on the domain, the email is delivered LOCALLY to the VPS and not to the shared server.

In PHP running on the web host VPS using the domain MyDomain.com:

$headers = "From: MyName <[email protected]>\r\n";
$headers .="Return-Path:<[email protected]>\r\n";
mail( "[email protected]", "Header", "Content", $headers);

How do I force mail() to do a MX lookup for the domain? How do I get the email delivered to the shared host and not the VPS?

I tried to do this but it didn't work:

ini_set("SMTP","123.456.789.012");

Where 123.456.789.012 is the ip to the shared host.

Shared host is hostmonster. Is there a way to specify the email box using the ip and username? [email protected]

Upvotes: 1

Views: 424

Answers (2)

davidethell
davidethell

Reputation: 12018

Drop the mail() function in favor of PHPMailer. It is way more flexible, is object oriented, much easier to configure with SMTP and has much better attachments support (if you need it).

To send your email in phpmailer you'll just need something like this to set your SMTP:

$mailer = new PHPMailer();
$mailer->Mailer = 'smtp';
$mailer->Host = '123.456.789.012';
$mailer->From = '[email protected]';
$mailer->FromName = 'Me Myself';
$mailer->AddAddress = '[email protected]';
$mailer->Subject = 'My subject line';
$mailer->Body = 'Your Body text here, in HTML if you set $mailer->IsHtml(true)';
$mailer->Send();

Upvotes: 1

Brad
Brad

Reputation: 163438

The issue is that your VPS server is configured to handle mail for your domain. Adjust your mail configuration accordingly. Also, Linux ignores the SMTP setting. Only Windows uses this.

Alternatively, you can use a class like PHPMailer which can connect to a remote SMTP server directly.

Upvotes: 1

Related Questions