password_verifyが上手くいかないとき

ログイン機能をつくっていた際に、何故かpassword_verifyが上手くいかなかったが、

create tableする際に、passwordの桁数が少なかったのが原因。
passwordを255にして

create table mail.users(
	username varchar(41) unique,
	password varchar(255)
)

再度password_hashしたら、上手く機能しました。

<?php 

session_start();

$mysqli = new mysqli('localhost', 'hoge', 'hogehoge', 'mail');

$status = "none";

if(isset($_SESSION&#91;"username"&#93;))
	$status = "logged_in";
else if(!empty($_POST&#91;"username"&#93;) && !empty($_POST&#91;"password"&#93;)){
	 $stmt = $mysqli->prepare("SELECT password FROM users WHERE username = ?");
  $stmt->bind_param('s', $_POST["username"]);
  $stmt->execute();

$stmt->store_result();

  if($stmt->num_rows == 1){
    $stmt->bind_result($pass);
    while ($stmt->fetch()) {
      if(password_verify($_POST["password"], $pass)){
        $status = "ok";
        $_SESSION["username"] = $_POST["username"];
        break;
      }else{
        $status = "failed";
        break;
      }
    }
  }else
  	$status = "failed";
}
?>
<h1>ログイン</h1>
    <?php if($status == "logged_in"): ?>
      <p>ログイン済み</p>
    <?php elseif($status == "ok"): ?>
      <p>ログイン成功</p>
    <?php elseif($status == "failed"): ?>
      <p>ログイン失敗</p>
    <?php else: ?>
      <form method="POST" action="login.php">
        ユーザ名:<input type="text" name="username" />
        パスワード:<input type="password" name="password" />
        <input type="submit" value="ログイン" />
      </form>
    <?php endif; ?>

php ログイン登録

db table

create table mail.users(
	username varchar(41) unique,
	password varchar(41)
)

register.php

<?php

$mysqli = new mysqli('localhost', 'hoge', 'hogehoge', 'mail');

$status = "none";

if(!empty($_POST&#91;"username"&#93;) && !empty($_POST&#91;"password"&#93;)){
	$password = password_hash($_POST&#91;"password"&#93;, PASSWORD_DEFAULT);

	$stmt = $mysqli->prepare("INSERT INTO users VALUES (?, ?)");
	$stmt->bind_param('ss', $_POST["username"], $password);

	if($stmt->execute())
		$status = "ok";
	else
		$status = "failed";	
}
?>
<h1>新規登録</h1>
<?php if($status == "ok"):?>
	<p>登録完了</p>
<?php elseif($status == "failed"): ?>
	<p>エラー:既に存在するユーザ名です。</p>
<?php else: ?>
	<form method="POST" action="">
	ユーザ名:<input type="text" name="username">
	パスワード:<input type="password" name="password">
	<input type="submit" value="登録">
	</form>
<?php endif; ?>

db

なるほど。

mysql・PHPで疑似的なメールボックスを作りたい

まずは簡易的な設計にしたいので、
Mysql側のテーブルは、(1)account、(2)send、(3)receive の3つぐらいでしょうか。
sendのテーブルとtoで指定したaddressのreceiveテーブルに、メール文を保存して、
送信者・受信者がお互いに見れるようにする。受信者は、メールを見たら、alreadyreadのフラグを立てる。

create table mail.account(
	id int unsigned auto_increment primary key,
	address varchar(255),
	passwords varchar(255),
        name varchar(255)
);
create table mail.send(
	id int unsigned auto_increment primary key,
	accountid varchar(255),
	to varchar(255),
	subject varchar(255),
	body varchar(255),
	file1 varchar(255),
	file2 varchar(255),
	alreadyread int,
	sendtime datetime default null
);
create table mail.receive(
	id int unsigned auto_increment primary key,
	accountid varchar(255),
	subject varchar(255),
	body varchar(255),
	file1 varchar(255),
	file2 varchar(255),
	to varchar(255),
	from varchar(255),
	alreadyread int,
	junk int,
	delete int,
	receivetime datetime default null
);

ああああ、singin・loginフォーム作らないと駄目だ。。

mb_send_mailでメールフォーム

投信の会社などは、メールフォームがないのがトレンドのようですが、
さて、mb_send_mailを使います。vagrant postfixで設定している為、fromの挙動が異なります。

form.php

<title>Form</title>
<body>
	<form action="send.php" method="post">
	件名:<br>
	<input type="text" name="subject" size="30" value=""/><br>
	送信者名:<br>
	<input type="text" name="name" size="30" value=""/><br>
	メールアドレス:<br>
	<input type="text" name="mail" size="30" value=""/><br>
	本文:<br>
	<textarea name="message" cols="30" rows="5"></textarea><br>
	<br>
	<input type="submit" value="送信する"/>
	</form>
</body>

send.php

<?php

$message = "名前:" .htmlspecialchars($_POST&#91;"name"&#93;)."\n本文:".htmlspecialchars($_POST&#91;"message"&#93;);
if(!mb_send_mail("hoge@gmail.com", $_POST&#91;"subject"&#93;, $message, "From:".$_POST&#91;"mail"&#93;)){
	exit("error");
}
?>
<p>メールが送信されました</p>

php-imapのインストール

Postfixのdependency(remi)を解消してsudo yum install php-imap後、php.iniでextension=imap.soを追加しても、
全くphpinfo()に反映されず、vagrant haltしたり、php.iniを編集したり、
他のサイトを見ながら5時間くらい色々試しても全然ダメで、
あきらめてカタ焼きそば食って再度vagrant upしてみたら、入っている!? 何故だ? さっぱりわからん。

とりあえずエラーは出ない模様

$imapPath = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = 'hoge@gmail.com';
$password = 'pass';
 
// try to connect
$inbox = imap_open($imapPath,$username,$password) or die('Cannot connect to Gmail:'.imap_last_error());
$mailHost = 'imap.googlemail.com';
$mailPort = 993;
$mailAccount = 'hoge@gmail.com';
$mailPassword = '';
$mailBox = imap_open('{' . $mailHost . ':' . $mailPort . '/novalidate-cert/imap/ssl}' . "INBOX", $mailAccount, $mailPassword);
if (!$mailBox) {
	echo '接続失敗';
}
echo '接続成功';

あれあれあれ

vagrantでのメール送受信

サーバーにグローバルなドメインが設定されていない場合、送受信できないとのこと。理由として、mail.hoge.localのドメインがLAN内のみ有効で世界のインターネット上では名前解決出来ない為。

個人情報に関わるからでしょうか、かなり厳しいですね。。

IMAPとは?

Internet Message Access Protocol(インターネット・メッセージ・アクセス・プロトコル)
メールを取り込む手順

->「POP」では、メールをいったんスマホやパソコンにダウンロードして取り込むと、サーバーから無くなってしまう
->「IMAP」では、ダウンロードするのはメールのコピー(キャッシュ)で、メール本体はサーバーに残ったまま

とりあえず、gmailのimapをonにします。

う~ん。。。

dovecot

1.インストール
sudo yum install -y dovecot

2.バックアップ
sudo cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.org

3.maildir形式
mail_location = maildir:~/Maildir

4.起動
sudo service dovecot start

smtpサーバーをsmtp.gmail.comで設定している場合、dovecot入れても意味ないのかな。
作りたいのはメールボックスそのものなんだが。

phpmailerで複数アドレス(配列)にBCCで送信

複数アドレス($addressの配列)をforeach の$mail->addBCC($value);で回せばOKですね。
$mail->addAddress(”);、$mail->addCC(”);は使わなければ、コメントアウトします。

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$address = array('target1@gmail.com','target2@hotmail.com','target3@yahoo.co.jp');

$mail = new PHPMailer(true);
try{
	$mail->SMTPDebug = 2;
	$mail->isSMTP();
	$mail->Host = 'smtp.gmail.com';
	$mail->SMTPAuth = true;
	$mail->Username = 'fromhoge@gmail.com';
	$mail->Password = 'app pass';
	$mail->SMTPSecure = 'tls';
	$mail->Port = 587;

	$mail->setFrom('fromhoge@gmail.com');
	// $mail->addAddress('');
	// $mail->addCC('');
	foreach($address as $value){
		$mail->addBCC($value);
	}
	
	$mail->addAttachment('test.gif');

	$mail->isHTML(true);
	$mail->Subject = 'Here we comes!';
	$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->send();
	echo 'messeage has been sent';
} catch (Exception $e){
	echo "Message could not be send, Mailer Error:", $mail->ErrorInfo;
}

BCCで問題なく届いてます。これは想像以上だな~