Using PHP to send mails from my organisation's email id? - php

So, I developed this web application which needs me to send emails to everyone (details in a mysql database), providing a unique set of user credentials. I have a virtual server on the University network where I am hosting my PHP based application, so it obviously doesn't have its own mail server/SMTP settings.
So, if I were to send out mails from a university network, I guess I should request the network admin for the SMTP settings for the university mail network and then use PEAR for it right?
I need to confirm this because it's kind of a big deal to make a request like that, and I myself have never used PHP to send mails 'from' an external email id not linked to the server.
Are there any alternatives to this?

Yes you can use PEAR::mail to send mails via SMTP server. However, if SMTP is configured on localhost you can simply use mail() function to send mail. You can also connect to SMTP server via socket but then you need to handle whole protocol communication.

Related

Postfix sending mails successfully but receivers don't get them

I've tried using Postfix only as a SMTP server for sending "verification" mails locally, to any provided destination. The current problem is, it sends all mails without any issues but receivers don't get them.
I'm using XenForo software with "127.0.0.1:25" SMTP data for sending mails, without user - / pass login because Postfix is locally bound and should only send mails - not receiving any from outside.
Also I'm using my custom domain to provide "from:" data for mail. (e.g. "no-reply#*******.com")
I already tried using SMTP servers of different hosts such as "MailJet" but I had the same issue there. The staff of used hosts said, there'd be something wrong with my server and nothing else, so that's why I had decided to host an own SMTP server locally on the same server as like as my webserver's running.
Postfix "main.cf" file: http://pastebin.com/r8EfKbXL
Logs: http://pastebin.com/TnjVbNfp
Thanks in advance.
Sincerely,
Chris
And you have SPF, DKIM, etc. all sorted? I suggest you (authenticate with and) use the same SMTP server you use for all other email from that domain, create a separate inbox if you need so it's using different credentials to you. It's usually a lot less hassle.

Are there any SMTP servers written in PHP or Go?

I don't mean SMTP/IMAP clients, I mean a working SMTP server that can both receive and send email.
There are lots of examples of partial PHP and Go SMTP servers that only listen for SMTP connections - aren't there any which show how to send mail?
I'm really interested in learning this second half of the Simple Mail Transfer Protocol. I understand that there are a lot of bad servers which vary from the standard - but I want to learn how the whole protocol from start to finish is implemented.
I think you misunderstand how SMTP is supposed to work. Here is a simplified version:
The Mail User Agent (MUA) queues a message for delivery by sending it to the Mail Submission Agent (MSA).
MSA connects to the Mail Transfer Agent (the "smtp server") over SMTP.
The MTA then uses DNS to lookup the MX record for the recipient's domain. It then contacts the recipient's MX server as a SMTP client.
The MX server accepts the envelope; it then forwards it to a Mail Delivery Agent (MDA).
MDA then puts the envelope in some message store where some IMAP or POP3 server reads messages. The MUA then connects to these servers to retrieve the message.
The entire process uses three main commands. MAIL, RCPT and DATA.
MAIL = Envelope information, bounce addresses, etc.
RCTP = The recipient.
DATA = The payload.
The SMTP server responds - much like HTTP actually, with error codes and based on that, the MTA knows what to do with the envelope (its a bounce back, so send appropriate reply, etc.)
In this process there is no such thing as "retrieve email" (ignoring ETRN for a bit); as SMTP is purely for email transmission and not retrieval.
I found a full SMTP server written in PHP - even includes a nasty open relay.
$ sudo php php-smtp.php [ip-address] [port]
There is no "second half" of SMTP, just the protocol. If your MUA interacts directly over TCP with the mail server (rather than using a helper program like the /usr/bin/sendmail binary found on most Unixes), then it uses the SMTP protocol. The MTA uses the same protocol to talk to other MTAs when delivering the mail. It may use a larger set of the available verbs, depending on the circumstances.
Seeing the code of a PHP or Go implementation of an MTA would show you how one person/team has implemented the SMTP protocol.
THere's a PHP smtpd server that just processes mail - https://github.com/flashmob/Guerrilla-SMTPd
and a port to go
https://github.com/flashmob/go-guerrilla
There is a non blocking SMTP server writen in PHP on top of ReactPhp:
https://bitbucket.org/david_garcia_garcia/smtpserver
It is designed for the end user to have custom Authentication and Delivery implementations, the rest of the SMTP behaviour works out of the box.

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 PEAR Mail: Relay access denied

I can send emails to myself and other people on my mail server using PEAR Mail. But when I try to send to another server, I get an error:
Relay access denied.
I've checked the SMTP configs and they're exactly the same as in my email client.
What could it be?
Relay access denied.
A "relay" is a mail server that acts as an open gateway, allowing anyone to connect to it and send mail through it ... to anyone. If the target isn't local, a relay will make a best effort to pass it on.
Obviously this is a tremendously bad idea.
Chances are that the mail server you are using is either locked down to local delivery only, or it wants you to authenticate first to prove that you are permitted to send mail through it.
You will need to contact your sysadmin to ask him or her about the error, and exactly what you need to do in order to send mail through it.
Is your email client authenticating to your SMTP server? Most servers will allow authorized/authenticated users to relay mail through them. That way you can be (say) a salesman in a hotel room somewhere and still send mail to a 3rd party via your corporate mail server.
Check that your PHP mail call is using the proper SMTP credentials (which means username/password, not just the server address). Since your email client can relay, but your script can't, most likely the script isn't authenticating.

Categories