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
2009-10-26 22:47:48
$to will always be my gmail account.
Mk12
2009-10-26 23:23:05
It doesn't work. No mail shows up in my gmail.
Mk12
2009-10-26 23:30:15
+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
2009-10-26 23:03:05
This is safer and easier way to send email than the raw mail functions. Great suggestion!
David Caunt
2009-10-26 23:06:34
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
2009-10-27 01:07:27
+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
2009-10-26 23:04:48
The "from" is just a plain email address, like '[email protected]', where would these 'mime headers' come frome?
Mk12
2009-10-27 01:10:37
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
2009-10-27 01:38:05
bucabay
2009-10-27 01:39:16