tags:

views:

439

answers:

8

I have a project to setup an anonymous feedback form to our website. I wanted to have a go at it with php

the general idea is that I would have a password field (give out to our employees) a field for the feed back and a button to

Ive seen this code snippet

<?php
$to = "[email protected]";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");
 }
?>

But im pretty sure with my limited understanding email systems that i can't just send out email (unless i have an open server somewhere which i do not) so i would need to log on to and email server (in my case gmails server)

so how would i do this?

update:

the code above does work but it doesn't come from a corporate account we currently use Google biz apps and that's why I would like to log on to a Google server to send this out.

A: 

Your understanding seems to be a bit too limited; it's perfectly possible to "just send" a mail from anywhere connected to the internet.

Williham Totland
Not just anywhere. I've a dynamically allocated IP address, so many mail servers will not allow me to send them mail. `gmail.com` for example will block my IP address and give an SMTP error.
Inshallah
How exactly would mail.gmail.com distinguish a dynamic IP from a static one?
Williham Totland
@Totland see SPF and SMTP dialback.
bucabay
I have a c# app that does the same thing so i know it works.
Crash893
A: 

Have you tested this? I think it should work fine, actually.

Litso
yes but its not gmail
Crash893
+1  A: 

If you're running your scripts on a proper web host, they'll have everything set up for you to send outgoing mail. It truly is as simple as shown there.

Things get a little more complicated if you're running a server at home, as ISPs often block home accounts from sending mail except via the ISP's intermediary servers.

ceejayoz
+1  A: 

php mail() will actually use the system's mail system to send email. If your host is using a linux base system, most of the time this is sendmail. As previously mention, many host already have this set up for you

See mail() requirements

Scott
A: 

If you're on a Linux server, with a properly set up mail server, the example given will work fine. If you're on decent web hosting, it'll be fine.

If you're on a development machine, you can set up a local mail server, and configure the mail server to send email through another mail server. It's usually called... smarthost, I think. Most Linux mail servers (exim, postfix, whatever) can be configured this way. I'm pretty sure you can install and configure a mail server on a Windows machine in this way as well. Xampp has one, I think.

If you actually want to send email directly to an email server using PHP, you can use a library. Personally, I'd use swiftmailer.

http://swiftmailer.org/ http://phpmailer.worxware.com/

They'll allow you to use any SMTP server you like, rather than just the local mail server.

BlackAura
A: 

Your code have no input filtering which is open to spam abuse, its best if you a thirdparty mailer like swiftmailer

Tutul
A: 

You can usually just use mail() if the server was configured for you.

In order to send email through GMail from your server, you need to send the email via SMTP to Gmails mail server (MTA).

The mail() function can be configured to use SMTP or use the sendmail binary on the server. It is best that you ask your web host to do it. If you want to do it yourself it requires editing your PHP.ini.

Sendmail is a deamon that has a command line interface to sending email. (note: all emails are sent via SMTP, sendmail just does that bit for you).

You will first need to configure your sendmail program with the google mail server authentication method, and your username and password. This is specific to the sendmail program you use. Then you need to configure PHP to use your sendmail program via PHP.ini. If you need specific instructions it can be posted.

I don't think you can configure PHP to use SMTP directly in this case, since google requires TLS and authentication parameters (user/pass) which you can't put into PHP.ini.

Note: The PHP docs mention that sendmail is for linux and SMTP is for windows only. This is NOT correct. You can configure PHP on windows to use sendmail, and linux to use SMTP. You can just need a windows version of sendmail, and linux comes with sendmail by default, hence you really don't need SMTP for it. Sendmail is usually a better choice since it will add the mail to its queue, while PHP is single threaded and has to wait for the SMTP transaction to complete.

bucabay