Hi I have been reading alot of articles adoring the PEAR mail package and it seems like PEAR is something I need to try out.
I am interested in setting up a full mail server, similar to a conventional SMTP mail service; which incorporates mail queuing, resending with a backend database etc. My impression is that PEAR can do this but can its service be used with mail clients like outlook to send mail, just as how any any smtp server daemon can where one would enter a portnumber, server name and/or security protocol?
Thanks
No, PEAR isn't going to magically solve these problems for you.
PEAR is a collection of PHP classes that are meant to solve common problems faced by PHP users. The Mail packages offer code for interacting with different parts Email systems. They do not contain code for creating email systems from scratch.
For example, form the Mail_Queue documentation
The Mail_Queue class puts mails in a temporary container, waiting to be fed to the MTA (Mail >Transport Agent), and sends them later (e.g. a certain amount of mails every few minutes) by >crontab or in other way.
The MTA in this case in sendmail, postfix, etc.
Another example, from the Mail_Mbox documentation
It can split messages inside a Mbox, return the number of messages, return,
update or remove an specific message or add a message on the Mbox
Incorrect use of "an" aside, you are using this to read existing MBOX files, and not caring how they got there.
The Mail package is about interacting with existing mail systems, NOT creating replacements. You'll still need to understand how all those email systems work to create a "full mail server, similar to a conventional SMTP mail service". If you're doing this because you want to learn how email systems work, have at it. If you're doing this because you thing this will give your business some leg up in the email game, I laugh and say "good luck with that"
PEAR is a repository for lots of libraries. Some of them deal with mailing.
PEAR's Mail class is designed for sending mail only. It is not designed as an implementation of an SMTP server.
Pear Mail is an SMTP sender aka client, not an SMTP server. Although it's entirely possible to write server (any kind of server) in php that doesn't mean that writing an SMTP server yourself is necessarily a good idea as it requires quite some expertise to do it right (spam anyone?). If you want to see an SMTP server implemented in a scripting language, go have a look at Lamson, written in Python by Zed Shaw.
And while you're there, do read the About page. This quote says it all
However, as great as Lamson is for
processing email intelligently, it
isn’t the best solution for delivering
mail. There is 30+ years of SMTP lore
and myth stored in the code of mail
servers such as Postfix and Exim that
would take years to replicate and make
efficient. Being a practical project,
Lamson defers to much more capable
SMTP servers for the grunt work of
getting the mail to the final
recipient.
It seems to me that PEAR's MailQueue package may address your needs.
Related
I have heard that all of these methods are valid ways to send email in PHP.
What are their advantages and disadvantages?
The three options aren't quite in the same league, presuming the first means the mail() function.
Using the mail() function usually calls the local mail injector, commonly a binary program supplied by the MTA actually called "sendmail". The problem with mail() is that it is a non-straightforward interface with a number of gotchas and traps which are not well documented. This is because it mimics (IMO badly) the call to the Unix CLI mail command.
It is possible to call the local injector yourself, but this documented even less well. You may as well call mail(), anyway, since this is what the latter does.
Using SMTP comes with its own set of problems, however. If there is a local MTA that accepts and forwards mail, then it is not a bad solution. If there isn't, you will have to figure out which host you should be sending to. This will either be figuring out which external host should be doing your forwarding, or doing the actual MX lookup yourself. You will also need to know the SMTP protocol and be able to handle refusals for any reason. And you have to decide how you handle the need to re-try the send.
Doing the SMTP yourself also has the problem of not de-coupling the email sending from the reason the email is being sent. If there is a delay, or a problem, you have a page that will appear to be stuck. Using the local injector hands the former problem to the MTA; all you've done is queue the email for delivery. But then you don't have to worry about things like re-sending.
These three solutions also do not help you assemble your message, such as rich content, alternate content and attachments. You have to do all that (and add the correct headers!) yourself.
The normal recommendation is to locate a library that does all that for you, is robust and has a decent API. The usual one is PHP Mailer. The advantages of this library is that it also does all the message assembly, as well as figuring out how to do the delivery. But it's main win is that it Just Send The Message, trying mail() and local SMTP and even remote SMTP if it must. All transparantly.
I'm switching a web app over to AmazonSES. We're using PHP 5.3, PHPMailer, and we tried using the AmazonSES pear package but for some reason it was unbelievably slow on the live server. We switched to the Amazon perl script thats use sendmail instead and it's much faster.
However, the Undisclosed Recipients feature (built in to AmazonSES pear code) was no longer available. No matter what I do, the only thing I can get working it by addressing an email as "To: Undisclosed Recipients <workingadd#domain.com>"
The problem is that the working address HAS to be there: php's mail() func require it, phpmailer requires it. The problem is, using noreply# fails, but workingaddress# works. While the recipient list is protected, we end up getting all the emails at that address.
However, the Amazon code managed to send mails without a primary address. I can't find anything in their code that suggests the answer, nor on the net.
Please help!
Oh, You want to send mail, Please careful that MTAs don't you as SPAM, if you want to know all of its rules please read all of rules of SpamHaus
Set up an interim relay server with postfix or sendmail. Have your application send emails to that server. Have that server connect to amazon and send the mail to it. Let it handle the authentication, etc. It works extremely well.
--David
is there a library/class/code-snippet/etc. that allows me to directly receive mail in php?
So that I don't have to run an additional sever in an other process and then have to somehow send the mails to the already-running php-process.
I've been looking around for a while, but results for "php" and "mail" or "mta"/"smtp" on google focus mostly on sending mail, or retrieving it using pop3 or imap...
[EDIT]
What I'm trying to do is forward the messages to an IRC-channel, so obviously when the IRC-bot (in PHP) isn't running, loosing the mails is not a big deal. However having a low latency between receiving the mail and posting it on IRC is.
I've never seen a compete SMTP server in PHP and it doesn't surprise me. I don't think you want to go that route. I can think of two other ways to do this:
Use procmail (or similar) with your existing SMTP server and make a rule that forwards the messages to your PHP script. Pretty simple to do and it will fire the script the instant the message is received.
Have the messages delivered to some existing mailbox, then have your PHP script continually poll it (via POP or IMAP) for new messages. When you see a new message, pass it to IRC and delete it. How long it takes the message to appear depends on how often you poll the inbox.
Writing your own SMTP server to act as a MTA is a big undertaking. You could take a look at http://cloudmailin.com. CloudMailin allows you to receive the incoming email as an HTTP Post and acts as the MTA sending the email direct to your PHP app. The PHP app can then process the email and send it to the IRC channel.
The MTA (Mail Transport Agent) is an application (i.e. sendmail, exim) that is used to move mail from location to location. As far as I know, there is no MTA coded in PHP. PHP offers classes and scripting that will handle mail transport, but it still processes through an existing MTA.
You should be able to configure the MTA to pass mail through a given PHP script to accomplish what you are looking for.
Writing your own SMTP server is a huge undertaking. Do NOT go this route. You'll waste an incredible amount of time duplicating work that's been done already. Choose one of the 'big' SMTP servers (postfix, exim, sendmail, etc...) and go with that.
Don't think that just setting up a dinky little script to listen to port 25 will do the trick. SMTP servers are incredibly complicated beasts and the mechanics of setting up that port 25 socket likely occupy less than 0.00000000000000000000000000000000001% of the work. (this number is totally true, I asked my gut what it feels and that's what came out).
Try this: http://www.php.net/manual/en/refs.remote.mail.php
10 seconds of googling. SMTP is for mail relay, although it is the defacto protocol for mail clients to send mail, due to the Unix heritage of every box running an SMTP mail relay.
POP3 and IMAP provide mail clients' access to mail.
I want to write an email transfer service and need a MTU replacement of sendmail/postfix.
I'm not looking how to deliver to a transmitting SMTP server (like a postfix listing on a SMTP port). I also don't need the receiving part of the server, bounces etc. would go to a different existing postfix.
All of it in pure PHP. SMTP is a pretty easy protocol but this would require access to the MX DNS record and other lot of details that need to be taken care of.
Why do i need this? Because most shared internet providers have insane low limits for sending out emails like 500 per day. Thats almost nothing if you want to setup even the lowest traffic email list.
EDIT: Please Note: The code needs to connect to the receivers SMTP server and deliver the message with an adapted header set (removed BCC list, added Path route). If you see a SMTP class less then 5000 lines or requires you to configure a SMTP hostip and port then this is not the stuff i'm looking for.
It needs to do all the stuff sendmail is doing just as a PHP library.
I use Pear's Mail class.
EDIT
After re-reading this, I see there's more to it than just a library. You are asking to send direct to the remote MX.
Why reinvent the wheel? Set up an instance of postfix on the server, which only listens to connections from the web server... and let an MTA do what it does best. Hand off the messages from php to a real mail server and move on.
In the case of ISP's that block outbound port 25 and force the use of a smarthost, this also allows you to restrict the rate of messages sent to the smarthost.
Lastly, sending straight to the end MX from your php script is a bad idea, because if you send to me, I'll never get it. I and many other sites use "greylisting" to reduce spam, which denies all initial requests with a 450 temporary error. Real MTA's will try again, but unless you implemented a delay queue and try again, your message will never go through.
We use http://sourceforge.net/projects/phpmailer/ to do SMTP email from PHP
SwiftMailer is the only library you'll need.
Try Zend Framework component Zend_Mail (you can use the component independently of the entire framework).
Here is something I wrote. It's quite minimal and I don't know how well it performs, but I wrote it with the intention of replacing sendmail, meaning that it will take a message, lookup all the MX records for the recipient domains, contact those mail servers and deliver a message for the corresponding recipients. It worked well enough for me at the time.
https://github.com/denvertimothy/ThriveSMTP
It's been a long time since I've used it, but I threw it up on Github just now.
I used http://www.mailerq.com/ which works cool. Its a queue based mail transfer agent. It requires rabbitmq.
It provides multiple worker as well.easy to store in database.
It provided management console as well.
worth to check it
i tried googling but sadly i get only documentations (or am i using bad keywords)
anyway
i can see that alot of programmers (even those im working with right now) does not seem to approve to using the php native mail function and resorts to using some other framework like sendmail? swift mailer etc...
i'd like to know why? are there really disadvantages to using the native mail function?
if so how does the mailing frameworkds solve that or how are they better??
There's nothing wrong with it for sending simple plain text emails.
However, once you get into multipart mime emails (say, you want an HTML version or to add an attachment) then you have to build the email yourself, and it can be quite tricky to get all the headers and encoding correct. In this case you're better off using a library.
The PHP manual for function mail mentions that there are some restrictions with the mail function and one of these are that the function opens and closes an SMTP socket for each email. The mail function works good when you just want to send a mail or two.
As far as I'm concerned, all of these problems pale in comparison to the major security problem:
Mail header injection: ( http://en.wikipedia.org/wiki/E-mail_injection , and php specific info: http://www.damonkohler.com/2008/12/email-injection.html )
Whereby a spammer bot spiders your site and, finding a vulnerability in your script that is easy to still have when using the very insecure mail() function, IS ABLE TO SEND EMAIL FROM YOUR SERVER TO AN ARBITRARY LIST OF CONTACTS, essentially turning your script & server into a cog in their spam email machine.
I recommend never using mail() with user input, and in general, just making use of PEAR::mail instead. http://pear.php.net/package/Mail/
Using PHP's mail() function requires a properly configured sendmail or equivalent on the host the program is running. However, the Windows implementation is a bit different. If you don't have your MTA configured properly, you won't be able to successfully send emails from your PHP scripts. Like another commenter said on this thread, PHP manual explicitly states that each call to the mail() function opens and closes a socket. This can cause unnecessary delay in script execution.
Additionally, your development and testing environment may not have a public static IP address. Your IP address might be blacklisted by DNSBL, Gmail, Yahoo! and other popular email service providers.
Your best bet in this situation is to use a properly configured external SMTP server. Chances are your employer has already provided an email account with SMTP access. If you don't have one you can use a Gmail account. Gmail provides SMTP access to all email accounts.
You can write scripts to open a socket connection to the external SMTP server. When there are tried and tested open source libraries for this purpose, why write your own?
Incidentally, I wrote a blog post on the very same subject yesterday: Using SMTP With Zend Framework - Solve Email Delivery Problem
Best regards,