【コピペでOK】PHPを使ってメールを送信するスクリプト

WEB開発 | 20, 02, 2019

PHP

# vim /var/www/html/sendEmail.php
<?php
   $to_email = "recipient@example.com";
   $subject = "Simple Email Test via PHP";
   $body = "Hi,nn This is test email send by PHP Script";
   $from_email = "sender@example.com";

   if (mail($to_email, $subject, $body, "From: $from_email rn")) {
      echo("Email successfully sent to $to_email...");
   } else {
      echo("Email sending failed...");
   }
?>
# php sendEmail.php

Html

<?php 
if (!isset($_POST["submit"])) { ?>
 
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
   To: <input type="text" name="to_email"><br>
   From: <input type="text" name="from_email"><br>
   Subject: <input type="text" name="subject"><br>
   Message: <textarea rows="10" cols="20" name="message"></textarea><br>
   <input type="submit" name="submit" value="Send Email">
  </form>
<?php
} else {
 
  if (isset($_POST["to_email"])) {
    $to_email = $_POST["to_email"];
    $from_email = $_POST["from_email"];
    $subject = $_POST["subject"];
    $body = $_POST["message"];
    $message = wordwrap($message, 70);
    if (mail($to_email, $subject, $body, "From: $from_email rn")) {
      echo("Email successfully sent to $to_email...");
    } else {
      echo("Email sending failed...");
    }
  }
}
?>

http://localhost/sendEmail.php

Writer

フクチ

フクチ| CEO


代表の福知です。 よろしくお願いします。






こちらの記事もおすすめ

Related Posts