简体   繁体   中英

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 there 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM