views:

123

answers:

3

Is there some way I can take text (retrieved from a form), and email it to my gmail account? I can also have the user enter their email address, and a subject. Or if not is there a better way to have users send me a message? Thanks.

+4  A: 

Use mail function to send email to specific address:

$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
mail("[email protected]", $subject, $message);

But please don't take $to parameter from form, otherwise your script will be used for spamming.

Ivan Nevostruev
$to will always be my gmail account.
Mk12
It doesn't work. No mail shows up in my gmail.
Mk12
+2  A: 

I recommend that you use PHP Mailer this program will take care of all of the message construction and works well with Gmail. There is also sample code included for Gmail.

meme
This is safer and easier way to send email than the raw mail functions. Great suggestion!
David Caunt
Thanks it worked!
Mk12
Only porblem was replying or from didn't work, but it doesn't matter, I just add the email address to reply to to the start of the body.
Mk12
+1  A: 

Expanding on what Ivan wrote to add users email as sender:

$subject = $_POST['subject'];
$message = $_POST['message'];
$from    = $_POST['from'];

// the sender email must be cleaned of mime header injection
$from = preg_replace_all("/[\r\n]+/", '', $from);

mail("[email protected]", $subject, $message, "from:$from\n");

This makes it easier to reply. However, you could just append their email address in the message body.

bucabay
The "from" is just a plain email address, like '[email protected]', where would these 'mime headers' come frome?
Mk12
The email address is a part of the mime message, just like how an email embedded in HTML becomes part of the HTML. So an email like: <i>[email protected]</i> becomes italic in HTML if not escaped of HTML entities. For mime, it is simple, just escape or remove \n.
bucabay
Eg: the email [email protected]\nbcc:[email protected] will send spam to [email protected]
bucabay