Use default PHP mail function with SMTP server on Linux - php

I am working on a php/c# project which is an Email Server with a web interface to manage the Email Server application.
The program is supposed to work on windows and linux but I have been mostly doing my development in windows.
I've not come onto testing in Linux and have found a horrible problem.
From what I have googled on Windows PHP you can choose an SMTP server that you want to use, but it looks as if on Linux you don't have this option so when PHP sends an email it completely bypasses my program.
Is there a way to make PHP use an SMTP server of your choice, I know you can use PEAR to overrride the SMTP settings but I'd prefer that the standard PHP mail function would work so other software like PHPBB forum would send emails via my SMTP server instead of the default php mail.
Is this something that is possible or is my only option to use pear?

You standard php mail function will just send to whatever is defined as the sendmail _path in php.ini
This is typically sendmail -t -i
You would need to configure sendmail to use smtp.
FWIW, most developer who do a lot of mail sending from PHP apps revile the mail() and instead use one of many mailing libraries (or services) which provide better configurability/reliability.
You could for example pipe the mail function to your own PHP script and use whatever library you wanted to in that script in order to do mail sending (and thus preserving the use of mail() function across applications).

Related

Send emails using PHP mail function, configuration of the mail server

I am making a web application (html + php + apache + mysql) which is on a Debian server, but when I try to send any mail using the php mail function, it is not simply sent me and throws an error. I guess I should install some smtp mail service. What do you recommend that is easy for a novice user settings?
I install a library called sendmail in localhost that simulates that smpt service in WAMP server, but when I upload the code to the production server simply does not send mail.
I researched on some mail servers like postfix, but the information I get is confusing to me. I want to recommend me some effective guide, or any server that is easy to configure for a novice user in the subject.
sorry if the question is obvious, but I want effective information
For sending a mail you just simply need a smtp client that sends a mail to a destination. You don't need to install any smtp server for just sending a mail.
A smtp server is for receiving mails( and other stuff about mails).
All you need to make php sending a mail is to install a smtp client like sendmail
then edit php.ini to be like this:
sendmail_path= sendmail -t -i
EDIT: I guess debian has sendmail by default.

Is it possible to set up PHP.ini to use the Yahoo Mail or Gmail smtp server?

Basically, every answer to this question that I have found recommends you download some external library. I don't need all the extra functionality when sending mails, all I want to do is set up php.ini to use Yahoo's servers, so I can use the mail() function. Is this even possible? And if not, what is the most lightweight no-frills way to use Yahoo Mail's smtp server? I would prefer it to be a single file if possible.
The mail() function sends the email through unix sendmail command or any other compatible MTA like postfix (Ubuntu's default MTA) or exim.
If you simply want to send an email through STMP, I recommend you using a 3th party library like SwiftMailer. Otherwise you will need to install and configure an MTA in your server.

Mail function - change SMTP relay

I use PHP's mail function to send emails from my server, which currently also handles mail. However, I would like PHP to start sending messages through a different SMTP relay, not the server it's running on. I checked out php.ini, and it appears that it is only possible to specify a different relay on a Windows PC, whilst my server is running OS X Snow Leopard. Is it possible to change the relay settings? Ideally, I wouldn't want to incorporate anything new into my PHP code, as I use the mail function all over the place.
You cannot do this without either setting up a sendmail alternative on your server that routes mail through SMTP, or much much easier, using a library like Swiftmailer.
PHP does not have the capability to use an external SMTP server built in (Unless you are on a Windows platform).

Sending mail via fsockopen?

I know this is possible, but can I do without a remote SMTP server or the like? Basically I want to send mail with PHP, but without mail()
I'm unsure what you exactly mean with 'without a remote SMTP server', as in any mail delivery at least one of those has to be involved - the one receiving mail on account of the recipient...
What you can probably do (it's up to you to decide if it's worth the effort) is to use PHP's socket functions to open a connection on port 25 with a remote mail server. Google 'SMTP telnet' for some examples of how a SMTP session looks like (quite simple, to be true) and then google for 'SMTP codes' for more explanations of what the remote server is saying you.
Possible, but not entirely trivial considering the fact that you should be familiar with SMTP, POP3 and/or IMAP to actually exchange data with a mail server.
You need to code your app so it mimics the behavior of an MTA, that is if you're going to do what the mail() function does - and using sockets. If you're on Linux, another option is to make an OS call to sendmail.
To not use mail() look into PHPMailer
I use this library for all my e-mailing code. I've extended it to have a debug mode so I can intercept outgoing e-mails while testing code.
I could be wrong but you will always be using an SMTP server even if that server is just the webserver with sendmail on it. If you were running your PHP on windows you'd need to enable IIS's in built SMTP service.

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