1

I've been trying to send an email using PHP, and I've found this code that suits my needs as it will easily fit into my current website's code. However, I put this entire code into a file and tested it out, and it won't send the email.

Code:

<?php 
if(isset($_POST['submit'])){
    $to = "heroicostrich@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

I am unable to get this to send an email to "heroicostrich@gmail.com". Am I doing something wrong, or did I forgot to fill something out?

joey942
  • 159
  • 2
  • 11
  • Are you using XAMPP, or something else for your local server? Chances are that you don't have a local mail server (such as sendmail) setup yet. These local environments almost never include a mail server, and so it needs to be setup separately. – Brian Schroeter Sep 18 '16 at 22:16
  • @BrianSchroeter I enabled POP for my gmail, but still have no idea what I'm doing with these mail servers. – joey942 Sep 18 '16 at 22:31
  • I'm happy to point you in the right direction. What software are you using to manage your local server? Common platforms are XAMPP, and WAMP. It could be a number of different things though. :) – Brian Schroeter Sep 18 '16 at 22:34
  • I don't even think I have a local server yet... (Sorry if I frustrate you haha, new to PHP and mailing stuff) – joey942 Sep 18 '16 at 22:42
  • No worries! We've all started somewhere, so don't let that get you down. Okay -- I think we can figure this out. Where are you currently putting your PHP files right now, for them to run? In other words, you have this PHP file on a server somewhere, or on your local computer. – Brian Schroeter Sep 18 '16 at 22:48
  • It's on my local computer, in a folder on my desktop. – joey942 Sep 19 '16 at 00:17
  • 1
    Ah, okay! I think we're getting to the root of things. If you have HTML files on your local computer, a simple web browser like Chrome can open those. However, PHP is a server side script, or programming language. PHP requires a web server, such as Apache, for it to process the commands within the code. Without a web server, PHP is unable to do anything at all. You can setup a server on your own computer, using free software such as XAMPP. – Brian Schroeter Sep 19 '16 at 01:28
  • Ohhhh thanks, kinda makes sense now. I'll check it out. – joey942 Sep 19 '16 at 03:40

2 Answers2

1

As, you are using it locally, and your mail server is not configured:

You can configure Gmail SMTP for that to work like this:

// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

It also occurs sometimes that either your sender email or recipient email should be an email that is configured on your website's mail server or cPanel. Due to security reasons, most hosting service providers do not allow this thing. Change it to an email address which is configured on your website's mail server, it should work then.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Pravesh Agrawal
  • 869
  • 1
  • 11
  • 27
  • Well currently I am running everything locally, it's not up and hosted yet. Is there any way I can get this to work without it being up and online? – joey942 Sep 18 '16 at 21:41
  • @joey942 client side? you mean locally hosted? unless you have a local mail server, this wont work –  Sep 18 '16 at 21:43
  • How can you send a mail without a mail server? Yes, there are alternatives like you can configure Gmail SMTP in your PHP! – Pravesh Agrawal Sep 18 '16 at 21:45
  • Locally hosted, yeah, whoops. Alright thanks for the info. – joey942 Sep 18 '16 at 21:45
1

the problem is not the code but you aren't able to send e-mail without a "SMTP" server! Link

hihebark
  • 5
  • 2