Search code examples
phphtmlemaillocalhostmamp

send email on localhost with PHP


I want to send an email on localhost but don't really know how to do it.

I tried some different ways but it doesn't work.

I used PHPMailer https://github.com/PHPMailer/PHPMailer/tree/5.2-stable as the mailserver but I think thats maybe wrong implemented or so.

Don't know if it's important but I use MAMP.

This is what I currently have:

<?php
if (isset($_POST['submit'])) {
  require("PHPMailer/PHPMailerAutoload.php");
  ini_set("SMTP","ssl://smtp.gmail.com");
  ini_set("smtp_port","465");
  $mail = new PHPMailer();
  $mail->SMTPAuth = true;
  $mail->Host = "smtp.gmail.com";
  $mail->SMTPSecure = "ssl";
  $mail->SMTPAuth = true;
  $mail->Username = "mail account";
  $mail->Password = "password for account";
  $mail->Port = "465";

  $mail->setFrom('receiver mail', 'TEST');
  $mail->addReplyTo('receiver mail', 'TEST');
  $mail->addAddress('recipient mail');
  $mail->Port = "465";

  $mail->isHTML(true);
  $mail->Subject = "test";

  // get text from input fields
  $email = $_POST['email'];
  $name = $_POST['name'];
  $address = $_POST['address'];
  $city = $_POST['city'];
  $number = $_POST['number'];
  $textarea = $_POST['textarea'];

  $bodyContent =
    "<p>Name: " . $name . "</p>
    <p>E-Mail: " . $email . "</p>
    <p>Telefonnummer: " . $number . "</p>
    <p>Adresse: " . $address . $city . "</p>
    <p>Anliegen: " . $textarea . "</p>";

  $mail->Body = $bodyContent;

  if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error' . $mail->ErrorInfo;
  } else {
    echo 'Message has been sent.';
  }
}
?>

Solution

  • Okay, i finally found the solution. I updated my code to this

    <?php
    if (isset($_POST['sendButton'])) {
      require("PHPMailer/PHPMailerAutoload.php");
      require 'PHPMailer/class.phpmailer.php';
    
      $mail = new PHPMailer();
      $mail->isSMTP();
      $mail->SMTPAuth = true;
      $mail->Host = "smtp.gmail.com";
      $mail->SMTPSecure = "tls";
      $mail->SMTPAuth = true;
      $mail->Username = "secret";
      $mail->Password = "secret";
    
      $subject = utf8_decode('test');
      $mail->setFrom('secret', $subject);
      $mail->addReplyTo('secret', $subject);
      $mail->addAddress('secret');
      $mail->Subject = utf8_decode('test');
      $mail->Port = "587";
      $mail->isHTML(true);
      $email = $_POST['email'];
      $name = $_POST['name'];
      $address = $_POST['address'];
      $city = $_POST['city'];
      $number = $_POST['number'];
      $sendText = $_POST['sendText'];
    
      $bodyContent =
        "<p>Name: " . $name . "</p>
        <p>E-Mail: " . $email . "</p>
        <p>Telefonnummer: " . $number . "</p>
        <p>Adresse: " . $address . ' ' . $city . "</p>
        <p>Anliegen: " . $sendText . "</p>";
    
      $mail->Body = $bodyContent;
    }
    ?>
    

    Besides I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON" Now everything is fine. Thank you for your help guys