How to find server domain name for SMTP from PHP? - php

I have cPanel servers which run the same script, but I've hard coded the server name in to the code as I can't find anywhere to grab the actual server domain.
Imagine 3 cPanel servers and these are the server domains:
apple.hosting.com
banana.hosting.com
carrot.hosting.com
While running a PHP script to send mail via SMTP, I need to connect to the server and want to use the server name that would be provided for secure connections.
I don't want to create an array of the IP addresses and then have it compare, as that won't work with dedicated IP addresses.
I tried ini_get('SMTP'), which just returns a useless 'localhost'.
I tried ini_get_all(null,false), and no joy.
I checked the $_SERVER global, but only the domain being accessed appeared.
The only thing that returned the server name was phpinfo().

It took a while to find this, and in composing the question, I found the answer. Rather than waste what I think is a good question (since I couldn't find the answer easily), here it is:
New as of PHP 5.3:
gethostname();
That's it, nice and sweet.
Read more here: PHP function gethostname

You could send yourself an email an look in the email header (In Outlook: Open the mail, View->Options, there is 'Internet headers)
The normal sub-domain is mail.domain.com, and another solution could be:
Follow this:
cpanel --> go to your mailing accounts -- > click on configure mail account -- > there somewhere in the page you will get the information about your smtp server
And on that page you should find the information.

Have a look at mxtoolbox
Other then that, you'll probably want to check stream-socket-client

Related

Sending mail from webserver to external mail server with local domain name

This is an issue I've encountered several time and haven't yet found a decent solution for:
Sending an e-mail from a webserver on e.g. "domain.com", to info#domain.com which is hosted on an external mail server e.g. Google Mail
In my case I always send from PHP over Apache and often on shared hosting, but I can imagine this is the same case on other frameworks.
These e-mails always seem to be delivered to the local mail server, even if I set the MX records on that server to point to the right external mail server.
A solution for this is to use an external SMTP server, but this isn't always easy when you're working with clients that either need to set-up a new e-mail account on their server and provide the SMTP details or sign-up for a third-party SMTP server.
What is the solution for this? Is there no way around SMTP?
Most emai/MTA server "autoconfigure" themselves. They guess list of local email domains (doimans with locally hosted mailboxes).
In sendmail case you can turn it off adding the following line in sendmail.mc:
define(`confDONT_PROBE_INTERFACES', `True')
Documentation : cf/README - confDONT_PROBE_INTERFACES
I have had the same issue many many times (in my case using PHP on a LAMP stack).
Try/check the following.
If you are using cPanel or similar, set the MX records to the external mailserver (Google apps etc).
Set up an SPF record to allow your hosting website to send email (this way no need to configure SMTP).
This may not be applicable but if you are using something like phpmailer. Set the property $mail->isMail(); so it tries to use your SPF allowed local mail() function to send the email. Sorry for going off into very specific advice, but might help in your particular situation.
Worth checking there are not similarly named local mailboxes on your hosting box.
Hope this helps!

How to get the Reply Emails ? Need a Mail Server?

As i'm knowing just about PHP and don't know at all about Mail Servers, what i want to know here is:
How can i get the "Replied" emails (from the receivers) for the mails i've send (by using PHP, with a address like xxxxxxx#gmail.com)
Does it need to setup my own Mail Server?
Unless using own Mail Server, is there any other easy way?
Honestly i'm not well understanding over IMAP or Email Protocols. Doing setup my own IMAP Server is far worse. Can it be done simply with a Web Hosting which supporting IMAP protocol? Or do i need to setup manually out from the start?
To be brief, i need a simple guide.
You only need a mail server if you don't already have a place where those reply-tos end up. Which adress are you sending them as? Where would you fetch the emails for those adress? You can ask PHP to log into any mail server that you'd fetch your emails from manually as well, so there shouldn't be a problem. But maybe I'm not getting you right.
Simply put the reply address to an email you own. For the example you gave to work, you would need to have the xxxx#gmail.com account.
Most people use something along the lines of mailer#youdomain.com, ans then create an account for that on their ma8lserver.
If you want to use your own domain, you don't need to run your own mailserver. Gmail, hotmail, and others have services that let you use their mailservers for your fomain, for free.

How to send email from a specific ip address?

I'm running a vps with cPanel/CentOS, And i want to dynamically choose the IP address to send an email from right inside the php code. ( i'm open to any weired ways )
Is there any way to do that? i would really appreciate some clear ideas as i'm not that good at exim and stuffs.
P.S. i already have available IPs in WHM.
Thank you
You can achieve this by using sendmail and passing parameters to define the configuration file to use. Inside the configuration file you can use the Masquerading And Relaying options together with CLIENT_OPTIONS(`Addr=aaa.bbb.ccc.ddd') to send via a certain IP.
When using PHP mail use the additional_parameters to specify the sendmail config file to use and in that config file use the above options to configure it.
PHP has no control whatsoever over the SMTP server that sends the mail. You can bind SMTP servers (sendmail, postfix, exim, etc...) to specific interfaces, but that's got nothing to do with PHP. PHP's involvement with the email sending process is purely to generate the mail and then hand it over to an SMTP server for actual delivery.
Here is a thought. If what you need is to send the mail from a specific IP you have control over, but where the impetus for that mail doesn't originate from that IP, but from some web interface or whatever, you could:
Add the mail details to a table on a DB with the desired IP address as a field.
Setup crons to run a php script on each box with those IPs.
Parse over the table with that script to find any mail that needs to come from that IP.
Send the mail.
I have a reseller account on a virtual host and all my domains for example are under the same IP number, then whatever domain I'm using to send an email, it will be sent under the same IP number, I think it is controlled by the smtp especification, you can configure your smtp to send email with another server where of course you have an account.
Also create a table to control what server you want to use to delivery yours email.
ClientPortOptions and DaemonPortOptions are special cases since multiple
clients/daemons can be defined. This can be done via
CLIENT_OPTIONS(`field1=value1,field2=value2,...')
DAEMON_OPTIONS(`field1=value1,field2=value2,...')
Note that multiple CLIENT_OPTIONS() commands (and therefore multiple
ClientPortOptions settings) are allowed in order to give settings for each
protocol family (e.g., one for Family=inet and one for Family=inet6). A
restriction placed on one family only affects outgoing connections on that
particular family.
Source: http://www.sendmail.com/sm/open_source/docs/configuration_readme/

A couple problems re: CodeIgniter emailer

I have some problems with the email system for CodeIgniter:
First, the emails I send out (registration, confirmations) are getting caught in standard spam filters in gmail and other mail clients. How do I get around this? How do companies like Facebook get their emails through consistently?
Second, the mailer is working locally but once we deploy it it no longer runs (doesn't send emails) but all the other forms run just fine. Anyone ever run into a problem like this?
Thanks for all the help!
I can't really answer your first question - it's not specific to CodeIgniter. You just need to make sure your email doesn't look like spam. In short - there's no way of guaranteeing your e-mail will not end up in a spam filter.
As for the second question, I expect your production server needs to be configured properly for email. You probably need to configure CodeIgniter to send email properly. I would suggest setting up an SMTP server (or using an existing one) rather than using the standard PHP mail which I think CodeIgniter uses by default.
Regarding spam, most organisations are very secretive about how they prevent spam (not wanting to publish information which helps the spammers) and in some cases they don't actually know - an obvious examlpe of this is bayesian filtering - but, for example, hotmail use a completely unaccountable army of volunteers to manually classify emails.
Do and get a copy of spamassassin and try to reverse engineer how the standard rules work. Obvious things to check are:
1) AVOIDING LOTS OF CAPITALS
2) don't mention the 'V' word
3) make sure you've got a current and restrictive SPF 1.0 policy published
4) make sure your sending from an address which has A and PTR DNS records
5) Do provide a reply-to and from email address which use your domain in the address
the mailer is working locally but once we deploy it it no longer runs
doesn't send emails
Which? These are 2 totally seperate things. If the code is falling over (if so why have you not provided the error details) then its likely a PHP version issue or a problem with the connection to the MTA (or the PHP mail config).
The latter is a problem with the MTA itself.
99.9% of problems reported as PHP mail failures have nothing to do with PHP and are problems with the MTA.
Enabled detailled error reporting for your MTA and see where it is failing.
C.
You may have to configure the email on your server differently than your local development environment. I've had to in the past.
There are two basic ways that PHP can send mail:
Via a UNIX program called "sendmail" (only on non-Windows servers and only if it is installed - check with your hosting provider)
Via a SMTP server.
If you've configured CodeIgniter to use SENDMAIL, check to ensure that the Sendmail path is correct. Your hosting provider usually provides this somewhere in their online documentation.
If you're using SMTP, you need to make sure that your server can contact the SMTP server. You can do this by logging into the server via SSH and typing "telnet your.smtpserver.com 25". If you get an error message about not being able to connect, you know you have a problem with your hosting provider connecting to your mail server.
I've been able to diagnose this problem by enabling logging on my production server (http://bit.ly/4pprd6) and adding log_message('error', $this->email->print_debugger()) right after I attempt to send a message.

Does the PHP mail() function work if I don't own the MX record

I'm not sure I'm using all the correct terminology here so be forgiving.
I just put up a site with a contact form that sends an email using the PHP mail() function. Simple enough. However the live site doesn't actually send the email, the test site does. So it's not my code.
It's a shared host and we have another site that has the same function that works perfectly, so it's not the server.
The only difference between the two is that the site that doesn't work just has the name server pointing to us and so the MX record never touches our server.
So my question is, could some one please confirm that the mail() function wont work if we don't have the MX record pointing to our server. Thanks
Yes. It will work just fine. I have a PHP script using the mail() function with the MX records set to Google Apps.
If the two scripts are on different hosts (it's a bit unclear from your post), then make sure that the host doesn't block some of the custom headers. I had issues with this when creating my script, but removing all but the From header fixed the problem.
Some hosts (Godaddy is the worst) block your use of sendmail and mail().
I generally use smtp to send emails from my php applications and with PHPMailer it's super easy. Many applications are using older versions of PHPMailer and sometimes updating it can help. It's also easy enough to add quickly to short scripts as well.
Hey guys thanks for the answers, it is really appreciated.
After ignoring the issue for a few months it has come up again, I did however find the answer to my problems.
Firstly, as you answers suggested, PHP and the mail() function were working as expected. The mail was getting sent.
The problem lies when the email is sent, it simply presumes that because its being sent from mydomain.com to *#mydomain.com email that the email itself is hosted on the same server, so it gets sent there instead and ignores the MX record.
OK it's a bit more complicated than that, but that is the general jist.
Edit:
Found a better version of the topic sendmail and MX records when mail server is not on web host.
The mail() function sends mail from the server hosting the script. Since many shared hosting providers host separate mail servers, and because the mail() function doesn't support any sort of authentication, many shared hosting providers block it.
If the site uses SPF, remember to include the sending site in your SPF record. For more info see here.
Yes, you could put in what ever you want in the 'from' field and it would still work.

Categories