Reputation: 1694
I am using php's mail function to send emails. The problem is that, emails from different domains do not get sent. Only emails from my domain are sent. For example when i send an email from [email protected] it is sent. When i send an email with from as [email protected] or username@gmail .com the email is not sent.
When the emails get sent, they end up in the spam section.
Which headers do i need to bypass this restriction? Or any other solution.
thanks.
Upvotes: 3
Views: 7306
Reputation: 509
Try adding the fifth parameter in your mail() function:
mail($to,$subject,$message,$headers,"-f ".$from);
NOTE: $from must be similar with the 'from' email address you've specified in the header.
Upvotes: 3
Reputation: 7194
You can use the PEAR mail function http://pear.php.net/package/Mail .By using this you can send e-mail from a exchange server.But if you use sendmail server then you can use the below code.The below code will not work for microsoft exchange server
Hope it help u.
<?php
//new function
$to=//receipent email addressaddress;
$message=//Message;
$nameto = "Name of receipent";
$from = "[email protected]";//sender address
$namefrom = "Test";//from name
$subject = "Subject of the mail";
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = "mail.example.com";//mail server address
$port = "25";//port
$timeout = "30";
$username = "test";//user name
$password = "****";//password of sender
$localhost = "1.2.3.4";//Ip address of mail server
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */
//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
if(authSendEmail($from, $namefrom, $to, $nameto, $subject, $message))
{echo "send";}
else{echo "failed";}
?>
Upvotes: 2
Reputation: 28174
It sounds like your SMTP server is not allowing relay. Talk to your mail server admin and ask if they will open relay for your application's IP address.
Upvotes: 0
Reputation: 190945
It depends on your SMTP server. This is likely to prevent spam from flowing out.
Upvotes: 6