tags:

views:

77

answers:

6

I'm working on my first PHP project, and it's going well. I've been able to figure out how to do most of what I need so far, but there's one big problem for me now: E-mail.

I'm on a shared server, so I can't install PEAR, and I can't modify my php.ini. According to my host's very limited documentation, I have to use SMTP so I can't (correct me if I'm wrong) use the mail() function. So does anyone have any suggestions on what to do? At this point I just want to send a basic text message, so where would I specify my SMTP port number, user, password and so on? Thanks

+3  A: 

Even if you cannot install PEAR components at the system-level, you can still download some, and bundle them in your application, properly setting the include_path so it contains the directory in which you've put those components -- see set_include_path(), about that.


Else, there are others non-PEAR components, that might be great for you ; for instance, I've heard that Swift Mailer is great.

And it seems it has at least some documentation -- including the following pages, that might prove useful, in your case :

Pascal MARTIN
+1  A: 

Chances are that your provider has configured the mail() command already to it uses the right SMTP server. Providers usually do that. I would try that out first.

If it really doesn't work that way, use a mailer class like phpMailer. With that, you can specify the exact SMTP server to use.

Pekka
Thanks! I wrestled with phpMailer for a while, and now it's working.
Reg H
+1  A: 

Might as well give it a try, especially if the documentation is limited - it could very easily be inaccurate or outdated.


// The message
$message = "Line 1\nLine 2\nLine 3";

// Send
mail('[email protected]', 'My Subject', $message);

Also, even when you can't use php.ini you can sometimes use ini_set() to set some things yourself.

Syntax Error
A: 
// ini_set("sendmail_from","[email protected]"); // Only use if you have to.
ini_set("SMTP","mail.server.com");

$to = '[email protected]';
$subject = "Subject";
$body = "Body Content";
$headers = 'From: [email protected]' . "\r\n";
$result = @mail ( $to, $subject, $body, $headers );
if (! $result) {
    $errors = error_get_last ();

    $error = "";
    foreach ( $errors as $k => $v ) {
        $error .= "\n{$k} = {$v}";
    }
    error_log ( $error );
}

PHP: mail

Brant
@Reg H - Remove the "@" to see the error without going to the error log.
Brant
Why don't you remove it yourself? As it will gag an error completely, even from error log too
Col. Shrapnel
I'm using "error_get_last()" to set in the error log.
Brant
+1  A: 

very basic code just to show you an idea. working though

function smtp($recipient,$subject,$content) {

  $smtp_server = "mail.com"; 
  $port = 25; 
  $mydomain = "mydomain.com"; 
  $username = "[email protected]"; 
  $password = "xxxyyy"; 
  $sender = $username; 

  $handle = fsockopen($smtp_server,$port); 
  fputs($handle, "EHLO $mydomain\r\n"); 

  // SMTP authorization 
  fputs($handle, "AUTH LOGIN\r\n"); 
  fputs($handle, base64_encode($username)."\r\n"); 
  fputs($handle, base64_encode($password)."\r\n"); 

  // Send out the e-mail 
  fputs($handle, "MAIL FROM:<$sender>\r\n"); 
  fputs($handle, "RCPT TO:<$recipient>\r\n"); 
  fputs($handle, "DATA\r\n"); 
  fputs($handle, "To: $recipient\r\n"); 
  fputs($handle, "Subject: $subject\r\n\r\n"); 
  fputs($handle, "$content\r\n"); 
  fputs($handle, ".\r\n"); 

  // Close connection to SMTP server 
  fputs($handle, "QUIT\r\n"); 
}
Col. Shrapnel
+1  A: 

Google App Engine

Maybe you could try out Google app engine's email service(It is not a PHP solution, but this scales really well and is cheap). You can email 2,000 recipients(8 recipients/minute) daily for free. After that you only have to pay $0.0001 per recipient (5,100 recipients/minute). I think this is really cheap and it works really well.

Email service

I developed a real simple mail service. You just simply curl(post data) to your app engine domain.

Quick introduction to google app engine

If you are interested in this solution here is a quick video introduction by Brett Stalkin explaining how to creat a simple guestbook using python's app engine sdk within 10 minutes. I think this is pretty amazing.

Code

app.yaml

application: nameofmyapplication #name of your application
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: myemail.py

myemail.py

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail

#config
sender = "[email protected]" # Your admin email adres.
secret = "/7befe053cf52caba05ad2be3c25c340af7732564" # needs leading /

class Email:
    @classmethod 
    def email(self, to, subject, body):
        message = mail.EmailMessage()
        message.sender = sender
        message.subject = subject
        message.to = to
        message.body = body
        message.send()

class MainPage(webapp.RequestHandler):
    def post(self):
        if not sender:
            self.response.out.write("Please configure sender.")
            pass

        to = self.request.get("to")
        subject = self.request.get("subject")
        body = self.request.get("body")

        if not mail.is_email_valid(to):
            self.response.out.write("to param is invalid email address.")
            pass
        if not subject:
            self.response.out.write("subject param is invalid.")
            pass
        if not body:
            self.response.out.write("body param is invalid")
            pass

        Email.email(to, subject, body)
        self.response.out.write("Message sent.")

application = webapp.WSGIApplication(
                                     [(secret, MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

Configure application

For example you have the following configuration.

app.yaml

  • application: nameofmyapplication # name of your application

myemail.py

  • sender: To your admin email address specified for app engine. For example [email protected]
  • secret: To secret url which to post data to. For example: 7befe053cf52caba05ad2be3c25c340af7732564

Uploading application to app engine

  1. Put all the code in a directory <path to your folder>.
  2. Upload the code using appcfg.py update <path to your folder>
  3. If successful you can access your app online.

Sending email using curl

The final step to test your app.

curl -d "to=<[email protected]>&body=<Hello World!>&subject=<Testing app engine>" http://&lt;nameofyourapplication&gt;.appspot.com/&lt;7befe053cf52caba05ad2be3c25c340af7732564&gt;

Where arguments between <> you have to specify yourself, off course omitting the <>.If successful the server should response with Message sent.

Alfred