2

I wanted to rotate the origin IP addresses in an email script, it is used to send notifications to my customers.

My server comes with 5 ip addresses, so I created an array of IPs and while sending the IP address changed randomly, in this array I do not include the main server IP.

This was working fine until last december, I have not made any change nor update (I am not sure if there was any automatic)

Here is the script:

 $iparray = array(
    '163.xx.217.xx',
    '164.xx.217.xx',
);

$ips = $iparray;
$keyip = array_rand($ips);



        $mail = new PHPMailer();
        $mail->IsSMTP(); 
        $mail->CharSet = 'UTF-8';
        $mail->SMTPDebug  = 0;                     
        $mail->SMTPAuth   = true;                  
        $mail->SMTPSecure = "none";               
        $mail->Host       = $ips[$keyip];      
        $mail->Port       = 26;             
        $mail->AddAddress($recipient);
        $mail->Username=$senderemail;  
        $mail->Password=$senderpass;            
        $mail->SetFrom($senderemail,$sendername);
        $mail->AddReplyTo($senderemail,$sendername);
        $mail->AddBCC('smtp@xxxxx.co');
        $mail->Subject    = 'the subject';
        $mail->MsgHTML('the message);

Note that $mail->Host was an IP randomly selected from the array, when checking on the message received, I used to get this:

spf=pass (google.com: domain of test@xxxxxxx.co designates 163.xx.217.xx as permitted sender) smtp.mailfrom=test@xxxxxxx.co

However, since December, the SPF check is being made on the server main IP, instead of any of the selected IP in the array, which I do not want.

This was working properly but all of the sudden it changed, could it be Gmail servers? Could it be something to be configured in the server?

In order to send emails, I use Exim, installed on a Centos server, managed via WHM.

I am not trying to spoof any address, all IP addresses are mine, I just need to make sure they rotate on every email sent, and I could do it but now I am not sure what to change to make it work again.

Thanks for all your help

Daniel lm
  • 77
  • 1
  • 9

4 Answers4

4

Binding to an IP happens when the socket is created. You can control this by setting the options that get passed to stream_context_create() in the PHPMailer's SMTP class:

$mail->SMTPOptions = [
    'socket' => [
        'bindto' => "$bound_ip:0",
    ],
];

Where $bound_ip is a literal IPv4/IPv6 address, the result of gethostbyname('your-domain.example'), etc. Using 0 for the port allows the system to select the port normally. See the bindto docs for more info.

Walf
  • 8,535
  • 2
  • 44
  • 59
1

A SPF check is not made through declaring "everything is fine" in the mail, but through DNS records. Have a look at a SPF checker like https://mxtoolbox.com/spf.aspx to see whether all five of your IP adresses are listed in the SPF record for your outgoing domain.

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • The asker's point was that the SPF records include the additional IPs, which is why they want to use them for outgoing SMTP connections. – Walf Jun 09 '20 at 01:14
1

This is more of a networking question rather than PHP. A remote host like Gmail will only see the outgoing public IP from your mail server. If you connect to one of the original random IPs but the outgoing traffic is being routed so that the traffic is originating from the server's main IP, then that's all that Gmail is going to see. You need to look into your networking configuration to see if there have been any recent changes in that regard.

EDIT: I'll add that my answer -is- somewhat vague, but that's because we don't know what your networking setup is like. You should probably ask this question over at ServerFault: https://serverfault.com/

jhilgeman
  • 1,543
  • 10
  • 27
  • The question shows that the asker is talking about public IPs, and knows they are the ones that Google's servers see. PHP has been able to specify IP address binding for outgoing connections for a very long time. – Walf Jun 09 '20 at 01:20
0

the solution was not directly in te PhpMailer, but as I am usin Exim as MTA, the answer is to set exim to read the IP from etc/mailips, and you can even map individual domains to any of your server IP address

Daniel lm
  • 77
  • 1
  • 9