I wonder if can send email from server A using ip address of server B I tried to send email from my application using bellow code but it returns "550 5.7.1 relaying denied".
function sendmail($p_subject,$p_message)
{
$ip = 'ipServerB';
$domain = 'domain';
$returnPath = 'contact@domain';
$to = 'to';
$subject = 'New Ticket : '.$subject;
$message = $message;
$header = "from:support@domain\nto:$to\nsubject:$subject\n$message\n.\n";
$fp = fsockopen($ip, 25);
$telnet = array();
$telnet[0] = "telnet $ip\r\n";
$telnet[1] = "HELO $domain\r\n";
$telnet[2] = "MAIL FROM:$returnPath\r\n";
$telnet[3] = "RCPT TO:$to\r\n";
$telnet[4] = "DATA\r\n";
$telnet[5] = $header;
foreach ($telnet as $current)
{
fwrite($fp, $current);
$smtpOutput=fgets($fp);
echo $smtpOutput.' ';
}
}
Don't roll your own mailer. Use a class such as PHPMailer. Sending emails manually will, more often than not, result in "unclean" mail data with a very high risk of being considered spam, and/or problems in dealing with errors and retries.
In this case for example you're sending nonsensical data such as a telnet
command (which should get you 502 5.5.2 Error: command not recognized
), the SMTP conversation is totally blind, and the session is truncated rather than closed correctly. If you ever succeed in sending an email this way, it will be mostly due to sheer luck, and you shouldn't expect to be able to repeat the feat with any reliability.
That said, "relaying denied" means that neither the address you're sending the mail from, nor the address you're sending the mail to, are under the control of the server you're using. Therefore, the server would need to "relay" the email to someone else; and since you're not an authorized user, it refuses to do so.
Try using SMTP AUTH commands to send the server a valid set of credentials.
For comparison, this is how you would do it with PHPMailer:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = $AddressB;
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = $UserNameOnServerB;
$mail->Password = $PasswordOnServerB;
$mail->From = 'yourfrom@example.com';
$mail->FromName = 'Ticket System';
$mail->AddAddress('joe@example.com', 'Joe Bloggs');
$mail->AddAddress('bob@example.com', 'Bob Smith');
$mail->IsHTML(true);
$mail->Subject = 'Ticket no. ....';
$mail->Body = '<h1>It works!</h1><hr /><p>This is the email</p>';
$mail->AltBody = strip_tags($mail->Body);
if (!$mail->Send()) {
die('Boo hoo: ' . $mail->ErrorInfo);
}
echo 'OK!';