PHP email text to gmail account
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 the开发者_StackOverflow中文版re a better way to have users send me a message? Thanks.
Use mail
function to send email to specific address:
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
mail("your@gmail.com", $subject, $message);
But please don't take $to
parameter from form, otherwise your script will be used for spamming.
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.
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("your@gmail.com", $subject, $message, "from:$from\n");
This makes it easier to reply. However, you could just append their email address in the message body.
精彩评论