Jump to content

Send mail from PHP, override php.ini


reakt

Recommended Posts

Hi guys, I'm sending mail from php running on a cheap webhost with the following:

 

$to = 'name@domain.com.au';
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From: Name <noreply@domain.info>" . "\r\n";
$subject = "Interest registered";
$message = "<!DOCTYPE HTML.. a bunch of html";
mail($to, $subject, $message, $header); 

 

As you can see I've set "from" in the headers. When I receive the email however, it says from: myusername@my.cheaphost.com on behalf of Name [noreply@domain.info]

 

I need to get rid of the on behalf bit as it looks completely unprofessional. I just want it to say from: Name [noreply@domain.info]

 

Obviously this is probably something to do with the setup of the server, some of which I have no control over. I have access to another mail server which I can use to send email, so I put this at the start of my code:

 

ini_set('SMTP', 'mail.myotherserver.com.au');
echo ini_get('SMTP');

 

Which returns mail.myotherserver.com.au however, when I use the mail function, it still comes from myusername@my.cheaphost.com on behalf of Name [noreply@domain.info]

 

Is there something I'm missing, is the mail function not tied to the SMTP value?

 

Thanks

 

 

Link to comment
Share on other sites

It is usually tied to that value, but I expect if there's an error it will fall back on the default. Does your server provider allow other smtp servers to be run through the default port? I know many ISPs in this area block access to any other SMTP server on port 25, but any other port is nifty with them. Have you tried a different port using:

 

ini_set("smtp_port","26"); //Or some other port number

 

 

 

Link to comment
Share on other sites

Hi Nudd, thanks for the reply.

 

I tried setting the SMTP port to 26, still no go. It did get me thinking though and I believe the server requires authentication before it will send. How would I use php to authenticate before sending? Thanks

Link to comment
Share on other sites

I've found that for SMTP authentication you have to use PEAR Mail. I get the following error:

Failed to connect to mail.myserver.com.au:25 [sMTP: Failed to connect socket: Connection refused (code: -1, response: )]

 

Port 26 times out and gives me this:

Failed to connect to mail.myserver.com.au:26 [sMTP: Failed to connect socket: Connection timed out (code: -1, response: )]

Link to comment
Share on other sites

OK I've gotten into the mail server configuration a bit and it looks like it's been setup to only allow mail to be sent from IPs on the local network. I can add an exception, but how would I find out what IP my cheap domain host is sending from?

Link to comment
Share on other sites

Don't most (cheap) hosts just use the same server (or at least ip address) as their web server? You could just ping that. Alternatively, ping the smtp server that's provided by your host, chances are it's one of those.

 

Link to comment
Share on other sites

I had the same problem on a host a year or so ago... if I recall, I got it fixed by adding the php-version header to the mail headers. Almost like if it didn't know what php version was sending the mail, it defaulted.

 

Example from the PHP manual

 

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

 

Maybe that will help?

Link to comment
Share on other sites

You can also try using the optional fifth parameter to the mail() function:

<?php
$to = 'name@domain.com.au';
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From: Name <noreply@domain.info>" . "\r\n";
$subject = "Interest registered";
$message = "<!DOCTYPE HTML.. a bunch of html";
$fifthp = '-f noreply@domain.info';
mail($to, $subject, $message, $header,$fifthp);
?>

 

Ken

Link to comment
Share on other sites

Thanks for all the responses guys. Unfortunately none of them worked. However I finally got a response from the helpdesk of my cheap host and they told me to send the email via SMTP (through localhost, not my other SMTP server) and it actually works! Problem solved :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.