-1

Hi I have this PHP script that I found on a blog

<?php
if(isset($_POST['submit'])) {

$to = "youremail@gmail.com";
$subject = "Forms";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

} else {

echo "blarg!";

}
?>

Which is run when the following is executed in HTML

<form method="POST" action="mailer.php">
Your Name<br>
<input type="text" name="name" size="19"><br>
<br>
Your Email<br>
<input type="text" name="email" size="19"><br>
<br>
Message<br>
<textarea rows="9" name="message" cols="30"></textarea>
<br>
<br>
<input type="submit" value="Submit" id="submitBTN" name="submit">
</form>

According to the blog all i have to do is put the html and php files onto my web server (Which i don't have so i can't test this). Will it send an email to the email specified in $to ? I've never used PHP but this doesn't really makes sense how it can just email someone once its on the web. Thanks for an explanation/if this script would work straight up!

harrison
  • 19
  • 1
  • 1
  • 8
  • You're always going to struggle if you don't understand the code you're putting on your site. This is also a security risk (the concept, not necessarily this code.) Yes, PHP can e-mail - all those newsletters and "activate your account" emails you get are sent by PHP or an equivalent language. – Mitya Apr 09 '16 at 13:37
  • Yes, it's that easy. Anyone can send mail to anyone, pretending to be anyone, etc. There are ways to secure the transport, and authenticate the recipient & sender, but they're not necessary mostly. – Rudie Apr 09 '16 at 13:47

3 Answers3

1

This should theoretically work, but you will have to configure PHP to use your email server. I would recommend using something like PHPMailer to send email, that is what I always do. PHPMailer allows you to specify your IMAP/POP3 email server host, username & password, and email port, in the same manner that your email client does.

Here is a link to information about using Gmail in PHPMailer.

This snippet (taken from the PHPMailer readme) shows how to configure your server:

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

Now, you can configure header information for where the email is from, and who it is going to:

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

You can even add attachments to the email:

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

Finally, we put the subject and body into the email:

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

Now, we send the email:

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
captainGeech
  • 302
  • 1
  • 5
  • 17
  • You don't need a mail server to **send** mail. Like `sendmail`. – Rudie Apr 09 '16 at 13:46
  • @Rudie When I tried to use it in the past, it required I have a mail server on the same machine. I must have made a mistake, sorry. PHPMailer is still, in my opinion, a much better solution. – captainGeech Apr 09 '16 at 13:47
  • I agree. PHPMailer is perfect. It takes 'all' the hassle out. But it's not necessary =) You don't even need `sendmail`. You could connect to the SMTP server and talk SMTP to it. It's very friendly. – Rudie Apr 09 '16 at 13:48
0

You will need to setup a mail server on your server in order to send emails.

Not sure where you need to specify this though for PHP to use..

Take a look here: php mail setup in xampp

(Xampp mentioned in the link is a program that acts as the server so that people can look at your webpages online)

Xampp: https://www.apachefriends.org/index.html

Community
  • 1
  • 1
John Doe
  • 113
  • 1
  • 17
0

I did the same task long time ago. I accomplished this trough XAMPP (php and apache) and gmail. Here you can find a video with the complete explanation.

IgorAlves
  • 5,086
  • 10
  • 52
  • 83