What are the requirements to send an email with PHP? - php

I'm developing a Web application with PHP 5.3.3.
I want it to send confirmation e-mails automatically.
I don't have a domain name yet.
I'm using Windows XP.
I don't have PEAR's Mail package.
I have hMailServer and is the mail server I want to use to send the e-mails.
My questions are:
Can I send e-mails without a domain name? (Is it absolutely necessary? I just want to test the application for the moment.)
If a domain name is necessary, do I also need to host the application with a Web Hosting Service Provider? Can I do it from my computer?(Remember, I just want to test the application.)
Do I absolutely need the PEAR's mail package? or can I simply use the mail() function?
I'm clueless about what I should do. I've made my research, I understand the parts but I can't make sense of the whole, I mean what resources are necessary and how to put it together. I've never developed any application that sends e-mail before. Please, help me.

Yes. You can set your "from" address to whatever you want. (Though note, the recipient server might detect it's forged and reject it.)
You can host from you computer, just make sure your network is not blocking port 25 outbound.
mail() alone will suffice as long as you have a local SMTP server that will handle your messages. (I'm assuming that's what the hMailServer product is.)

First off, if you're running Windows as your server, you need to configure how your e-mails will be sent (SMTP server). Go to this section in your php.ini file. Change localhost to an SMTP server you can use from your network (either the IP address of your hMailServer OR sometimes you need to purchase one to use like http://www.smtp2go.com/).
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
Second, you use the mail(...) function in php (including the ability to send HTML mail). That's it.

Yes, you can. I believe your emails will show up with your server's DNS by default, but you can set the from: header to whatever you want. However, if you change the from: header, be warned that your emails are likely to get picked off by spam filters.
You need some special server configuration to use the php mail() function - I haven't tried setting it up myself, but I know that a basic local apache / php install won't have the capacity to send emails. Pretty much every paid web server should have it set up properly, though.
No. You can send regular and html emails with the generic mail() function.

Related

My SMTP service is not sending messages to external addresses

I have a local server which host PHP applications. My applications send emails to different addresses. However, I am noticing a problem that I am unable to figure out. It send emails to internal addresses but it won't send messages to external addresses.
I also have an exchange server on the same network. so for example I have these 2 "internal domain"
domain1.com
domain2.com
if I execute this php function from my application it gets delivered with no problem
mail("name#domain1.com","Test Subject","Test message");
However, if I try sening a message to gmail, yahoo or any external domain it does not send it
mail("name#gmail.com","Test Subject","Test message");
My server is running on Windows 2008 Server R2 with PHP5.3.19 installed on it via IIS6
How Can I correct this issue?
mail() on Windows needs extra config in php.ini because Windows does not have Sendmail or anything similar.
The first is the SMTP setting which should point to your ISP's outbound server which is not necessarily the same server that you retrieve your mail from. When I worked in support there were a lot of issues like you describe because an inbound server will still accept mail, but only for the domains that are local to it.
The second is the sendmail_from setting, which is what the gimpy little mailer PHP wrote for the Windows distribution uses in every SMTP transaction regardless of what's in the headers. On *nix Sendmail will read the headers and get the From: address itself, on Windows PHP will always use what's in php.ini. If this is a "garbage" address like example#example.com or something with a non-existant domain many mail servers will reject it outright. What I recommend is to change it to something decent in PHP.ini as well as adding the following line to your mailing scripts:
ini_set('sendmail_from', $from_address);
Alternatively, just use PHPmailer since it simply works much better than PHP's horrid little mail() function.

php mail() function not working

I'm getting a 'could not instantiate mail function' error from PHPMailer. From reading around, I understand this to mean that the PHP mail() function isn't working for some reason.
The results of phpinfo() for the mail settings are:
To me, this means that mail() should work and that port 25 is open. Is that right?
Is there anything else I can check to make this work please? I had a look at the docs for the mail() function, but I couldn't see what exceptions it threw and how I'd print them out to screen. I did a:
mail('name#email.com', "test", "test") or die("Doesn't work");
type test, but that's my error message and I could do with something a bit more helpful.
Grateful for any help on this.
Many thanks
it doesn't mean port 25 is open, it just means that PHP should use port 25 for contacting the SMTP server. You don't state what OS you're on, but note that sendmail would be a unix-only thing, and will fail if you're on Windows.
That list merely show you your current settings. That doesn't mean that they are right. :)
Your localhost is probably not configured to be a mail server. Set the smtp server to a real server than can be reached from your PHP server.
I am probably way off, but check to see if sendmail is installed, maybe it is malfunctioning. This depends on your OS.
The settings from phpinfo() show the PHP is set up to use SMTP but it does not mean that you have an SMTP server set up on the machine. Your error message suggests that one is not setup.
Good luck
My answer would be - don't send emails by calling Sendmail. The sendmail method (or ANY local method) is a mess of pitfalls... and even if you get past those issues, the bottom line is many spam filters (at the places you send mail TO) simply do not like this type of mail.
To provide just a little detail why the sendmail approach is bad, your sendmail daemon is unlikely to be configured to have an SMTP HELO which matches the reverse DNS of your IP address. Your webserver is unlikely to have valid reverse DNS matching a standard hostname. NO reverse DNS at all is bad, as is rdns like 123-123-123-123-static.someisp.com. SpamAssassing will flag such "unconfigured or default reverse DNS" hosts for example.
Fortunately you don't need to understand or fix everything I just said. The much simpler to accomplish (and easier to test/debug) is to GATEWAY your emails through a working SMTP mailserver. To do this:
1a) Install PHPMailer http://phpmailer.worxware.com/ ... OR
1b) Install the PEAR Mail() library http://pear.php.net/package/Mail
Either 1a or 1b will replace the limited "mail()" function in PHP. These replacements support both SMTP, and Authenticated SMTP.
2) I suggest using Authenticated SMTP over plain SMTP. Either works, but with authenticated SMTP you can literally send mail through another mail server just as IF your script were a local email client like Outlook. This has major benefits. For example, if you are a company sending mail, your mail is more likely to be trusted by remote/target mailservers, since your mailserver has a good reputation and (hopefully) proper reverse DNS setup. But if you originate the email off a webserver, you have none of that (and if you use shared webhosting, you will inherit the email reputation of whatever other sites run on your webserver IP.).

Sending Mail using PHP Mail function - Local mail server?

I am trying to send an e-mail from localhost but am getting the error:
Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().
Does this mean i need to set up a local mail server?? and if so how and which one is easiest.
Thanks in advance
Paul
This is what I use, but it's for testing purposes only:
http://www.toolheap.com/test-mail-server-tool/
There's almost no configuration, and I got it to work right out of the box (on Win7) after failure with several other mail servers. It does not send the actual mail, but stores it as an .eml file. This is great for testing mailing lists for instance, where you don't really want to send the 2000 emails, but want to get a realistic result from your application.
It might look like garbage, but it's been working great for me.
Yes, it does, and if you want to send mail to an outside email address (and not a user local to the system, it is annoyingly difficult).
Most hosting companies (e.g. GoDaddy) have this setup for you, so PHP's mail() function works without you needing to do anything.
If you're configuring it on your own system, you might want to consider (a) configuring sendmail to use an alternate SMTP gateway (I frequently use Gmail) or (b) a complete alternative to sendmail (Zend Mail looks promising.)
If you are using Linux, there is usually no need to set up a a mail server,
If you are using Windows, yes, you do need to set up a mail server
If you are interested in just sending mail, you can by SMTP protocol use any SMTP server. Here is a tutorial to setting up PHP to use a remote SMTP server.
Yes, you need a mail server to be able to send mail, but even if you do, you are not going to be able to send to addresses outside of you local network as mail from your computer will be blocked by all recipients for spam reasons.
You could use the pear mail function to connect to an external smtp providor like gmail to send the mails for you. More info here and here.
smtp4dev is in my opinion the best tool for capturing local SMTP traffic on Windows.
It listens SMTP on localhost port 25 and pops up a notification every time a new mail is posted (it doesn't actually forward the mail to its recipient). You can then open the message in your favorite mail agent or save it to a file.

PHP mail() not working

I'm building a site on my home computer using MAMP. The code I'm using employs the PHP mail() function to send emails, but whenever I test it, the mails aren't getting sent.
My computer is connected to the net, but I'm wondering if there's something about local hosting that prevents mails from getting sent. I'm not getting any kind of error message.
Any ideas?
PHP can send mail in one of two ways.
The first, and the default on non-Windows systems, is to use the local mail transfer agent installed on the system. This would be "sendmail" or an application compatible with it, the most popular probably being postfix.
The other is to connect via SMTP to some mail server.
You will either need to install a mail transfer agent on your local system (and set it up correctly), or edit PHP's configuration to specify an SMTP server address and port.
Yes, there are things that could block locally hosted mail. For one, your ISP could block SMTP to servers other than the ISP. ask your ISP support if they block SMTP... Or try telexing so someone's MX port 25 and do you get a response?
If your ISP blocks smtp you can still send the mail, but first you must relay that email through a hosted email server like your ISP mail server. This process is called 'smart hosting' and you can search for more info.
Even if you are not blocked on port 25, many sites will refuse or lose smtp traffic that originates from a dynamic or residential IP address, so again the smart host suggestion.
Also I suggest not using the built in mail() function in PHP... Use something that replaces and improves it like http://pear.php.net/package/Mail or http://sourceforge.net/projects/phpmailer/. Again, use the SMTP method as it is way more reliable than direct sending or calling Sendmail.
It is important to confirm this problem, doing SMTP manually over telnet. That way you isolate the problem from PHP. I did ISP support for years and saw this question lots. Most people setup php and mail correctly but get stuck on a background network issue with SMTP.
If you have Wireshark installed, it can record network traffic and you might see the actual SMTP traffic, for example the remote server may be refusing your connection. Wireshark is helpful but not required to solve this though. Good luck.
You need to setup SMTP server in order to be able use mail function, or you can use PHPMailer class, with it you can avoid using mail function and setup problems, PHPMailler need socket extension to be loaded in order to function correctly.

PHP: how to send email basics

I'm would like to use PHP to send email from my localhost to other people. What do I need to do that?
For example do I need to install mailserver? If I'm not mistaken there's is a language that you don't need a mailsever to send email. Is it right?
Inside the PHP.ini, there is [mail function]. How to configure this? I checked on the Internet, but do not really understand how it works.
[mail function] ; For Win32 only.
SMTP = localhost
smtp_port = 25
sendmail_from =admin#localhost.com //Not sure how to write this?
You would have to set up a local mail server if you want to send mail using the mail() function. You can't use a remote mail server as the php mail() function does not allow you to specify authentication credentials. However, I have found setting up a local mail server tedious and annoying, in addition it can be dangerous. I recommend looking into PHPMailer. It is simple to use and get running.
You need the software that will actually send the email after your PHP script has made a request to so (through using the mail function: http://php.net/mail). As stated in some of the previous responses there are software options for this, regardless of what operating system you run.
This, however, can sometimes be quite tricky for a beginner. Typically your ISP will give you access to an SMTP server from which to send emails, and you can set up your configuration to do this. For development purposes, this ought to do the trick for you. These details will likely be on your ISP's website (or possibly in your email client, somewhere.) Your config would end up looking something like
[mail function] ; For Win32 only.
SMTP = smtp.my-isp.com
smtp_port = 25
sendmail_from =my.account#my-isp.com
Failing that, you could just upload your script to your web host, where it should already be configured to work.
Hope that helps.
mail("recipient#domain.com", "Subject", "This is an email!");
You just need to install some email server. If you are on linux, you can try exim, if you are on windows, you can use the SMTP server that comes with IIS.
Yes, you do need a mail server available. PHP's mail() function accepts the information you give it and passes it off to the mail server for delivery. PHP doesn't deliver mail itself.
What mail server you use depends on what operating system you are using. Traditionally on unix-type machines there will be an installation of sendmail or some other service running. On Windows, you can specify the name of an SMTP server in the php.ini configuration file.
phpmailer is a good choice.
you can google it for details.
Actually, email is sent using socket.
The simple way is by using the mail() command. On Linux it's a pipe to sendmail binary and on Windows, I don't know, probably it use some Microsoft voodoo library.
Anyway, I strongly recommend to use phpMailer because it's a mature project, really stable, easy to setup, with a lot of features and it also includes an SMTP and IMAP client implementation, so absolutely cross-platform.
Anyway, you should consider to use anyway a local SMTP server as first hop to handle the mail queue in case of network failure.
There is no need for an installation of a special module to have access to mail functions in PHP.
But For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following:
/usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib.
It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.
If you are working on a linux environment using a hosting provider is most likely that the sendmail is already present, otherwise you can check from a terminal doing:
cat some_file.txt |mail -s "test mail" user#yourmail.com

Categories