<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/***** メールをSMTP経由にする *****/
function haup_send_mail_smtp_auth($phpmailer){

//設定値の変数化
	//メールサーバーのホスト名
		if(isset( GetOP04_optimize()['smtp-host-name'] )){
			$host_name = sanitize_text_field(GetOP04_optimize()['smtp-host-name']);
		}else{
			$host_name = '';
		}
		
	//SMTP暗号化方式
		if(isset( GetOP04_optimize()['smtp-encryption'] )){
			$smtp_encryption = absint(GetOP04_optimize()['smtp-encryption']);
		}else{
			$smtp_encryption = '';
		}
		
		if( $smtp_encryption == '1'){
			$smtp_encryption = esc_html('ssl');
		}elseif( $smtp_encryption == '2'){
			$smtp_encryption = esc_html('tls');
		}else{}
	//SMTPポート番号
		$port_number = absint(GetOP04_optimize()['smtp-port']);
	//ユーザー名
		if(isset( GetOP04_optimize()['smtp-user-name'] )){
			$smtp_user_name = sanitize_text_field(GetOP04_optimize()['smtp-user-name']);
		}else{
			$smtp_user_name = '';
		}
		
	//パスワード
		if(isset( GetOP04_optimize()['smtp-password'] )){
			$smtp_user_password = sanitize_text_field(GetOP04_optimize()['smtp-password']);

		}else{
			$smtp_user_password = '';

		}
		
//サイト名などの抽出＆変数化
	$blog_name = esc_html(get_bloginfo('name'));
	$blog_admin_email = sanitize_email(get_bloginfo('admin_email'));
	$reply_email = sanitize_email(get_bloginfo('admin_email'));
	$reply_name = esc_html('Re:');

	$phpmailer->isSMTP(); //SMTP有効化
	$phpmailer->Host = $host_name; //メールサーバーのホスト名
	$phpmailer->SMTPAuth = true; //SMTP認証の有無（true OR false）
	$phpmailer->Port = $port_number; //SMTPポート番号(ssl:465 tls:587)
	$phpmailer->Username = $smtp_user_name; //ユーザー名
	$phpmailer->Password = $smtp_user_password; //パスワード
	$phpmailer->SMTPSecure = $smtp_encryption; //SMTP暗号化方式（ssl OR tls）
	$phpmailer->setFrom($blog_admin_email, $blog_name);//送信者情報
	$phpmailer->addReplyTo($reply_email,$reply_name); //返信時の宛先と送信先アドレス
}
add_action('phpmailer_init', 'haup_send_mail_smtp_auth');