Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用phpmailer在表单中未插入邮件时出错_Php_Phpmailer - Fatal编程技术网

使用phpmailer在表单中未插入邮件时出错

使用phpmailer在表单中未插入邮件时出错,php,phpmailer,Php,Phpmailer,我遇到了表单问题,当用户邮件未插入时,它总是返回错误消息“无效地址:电子邮件”。我知道$reg_email变量正在其他函数中用于向用户发送邮件,因此我意识到我必须编写一个条件,说明当reg_email变量为空时,它必须返回一个不会导致该错误的值。但我不知道如何将这种逻辑转换成PHP。我该怎么办 <?php ini_set('display_errors', 1); session_start(); if($_SESSION['input_flag']) { unset($_S

我遇到了表单问题,当用户邮件未插入时,它总是返回错误消息“无效地址:电子邮件”。我知道$reg_email变量正在其他函数中用于向用户发送邮件,因此我意识到我必须编写一个条件,说明当reg_email变量为空时,它必须返回一个不会导致该错误的值。但我不知道如何将这种逻辑转换成PHP。我该怎么办

   <?php
ini_set('display_errors', 1);
session_start();
if($_SESSION['input_flag']) {
    unset($_SESSION['input_flag']);
}else{
    header('location: /');
}

$path = realpath(dirname(__FILE__) . '') . "/../";
include_once($path.'app_config.php');
include($path.'libs/meta.php');
//設定
require('./jphpmailer.php');
$script = "index.php";
$gtime = time();

$reg_name = isset($_POST['f_name']) ? htmlspecialchars($_POST['f_name']): "";

if (isset($_POST['f_company']) && !empty($_POST['f_company'])) {
    $f_company .= "■会社名"."\r\n" . $_POST['f_company'];
}

$f_adress = isset($_POST['f_adress']) ? htmlspecialchars($_POST['f_adress']): "";

$f_select = '';
if (!empty($_POST['select'])) {
    foreach ($_POST['select'] as $key => $value) {
            $f_select .= "設置されている消防設備"."\r\n" . $_POST['f_select'];
    }
}

$f_tel = isset($_POST['f_tel']) ? htmlspecialchars($_POST['f_tel']): "";

// $reg_email = isset($_POST['f_mail']) ? htmlspecialchars($_POST['f_mail']): "";

if (isset($_POST['f_mail']) || !empty($_POST['f_mail'])) {
    $reg_email .= "email"."\r\n" . $_POST['f_mail'];
}


$f_select2 = '';
foreach ($_POST['select2'] as $key => $value) {
    $f_select2 .= $value."\r\n";
}

$f_request = isset($_POST['f_request']) ? htmlspecialchars($_POST['f_request']): "";


$aMailto = array(

    "xxxxxx"
);
$from = "xxxxxx";
$fromname = '';
$subject1 = 'test';
$subject = 'test';
$entry_time = gmdate("Y/m/d H:i:s",time()+9*3600);
$entry_host = gethostbyaddr(getenv("REMOTE_ADDR"));
$entry_ua = getenv("HTTP_USER_AGENT");


$msgBody = "";
$msgBody .= "

■お名前
$reg_name

$f_company

■建物の所在地
$f_adress

$f_select
■お電話番号
$f_tel

$reg_email

■ご希望の連絡方法
$f_select2
■お問い合わせ内容
$f_request
";

//お問い合わせメッセージ送信
$subject = "ホームページからお問い合わせがありました";
$body = "

登録日時:$entry_time
ホスト名:$entry_host
ブラウザ:$entry_ua

ホームページからお問い合わせがありました。

$msgBody


";


//Message for the user
$subject1 = "お問い合わせありがとうございました";
$body1 = "

$reg_name 様






$msgBody



";

// メール送信
mb_language("ja");
mb_internal_encoding("UTF-8");

$fromname = "";


//お客様受け取りメール送信
$email1 = new JPHPmailer();
$email1->addTo($reg_email);
$email1->setFrom($from,$fromname);
$email1->setSubject($subject1);
$email1->setBody($body1);

//if($email1->send()) {};

//Anti spam advanced version 2 start: Don't send blank emails
if( $reg_name <> "" && $reg_email <> "" ) {

  //Anti spam advanced version 1 start: The preg_match() is there to make sure spammers can’t abuse your server by injecting extra fields (such as CC and BCC) into the header.
  if( $reg_email && !preg_match( "/[\r\n]/", $reg_email) ) {

    //Anti spam part1: the contact form start
    if($reg_url == ""){

        // then send the form to your email
      if($email1->Send()) {};
    } // otherwise, let the spammer think that they got their message through
    //Anti spam part1: the contact form end
  }//Anti spam advanced version 1 end
}//Anti spam advanced version 2 end: Don't send blank emails



//メール送信
$email = new JPHPmailer();
for($i = 0; $i < count($aMailto); $i++)
{
  $email->addTo($aMailto[$i]);
}
$email->setFrom($reg_email, $reg_name."様");
$email->setSubject($subject);
$email->setBody($body);

//if($email->Send()) {};

//Anti spam advanced version 2 start: Don't send blank emails
if( $reg_name <> "" && $reg_email <> "" ) {

  //Anti spam part1: the contact form start
  if($reg_url == ""){

      // then send the form to your email
    if($email->Send()) {};
  } // otherwise, let the spammer think that they got their message through
  //Anti spam part1: the contact form end
}//Anti spam advanced version 2 end: Don't send blank emails




?>

这是因为您正在这样做:

$reg_email .= "email"."\r\n" . $_POST['f_mail'];
单词email,后跟一个换行符,后跟一个电子邮件地址本身不是有效的电子邮件地址,因此将被拒绝,并显示您看到的错误。您还将附加到尚未定义的变量。改为这样做:

$reg_email = $_POST['f_mail'];
$email->setFrom('myemail@example.com', $reg_name."様");
$email->addReplyTo($reg_email, $reg_name."様");
我也对这一点感到困惑:

$email->addTo($aMailto[$i]);
PHPMailer使用的是
addAddress
,而不是
addTo
,因此除非这是
JPHPmailer
子类中的新方法或别名,否则它是错误的

另外,不要这样做:

$email->setFrom($reg_email, $reg_name."様");
这是伪造的,将导致收件人退回您的邮件或过滤垃圾邮件。改为这样做:

$reg_email = $_POST['f_mail'];
$email->setFrom('myemail@example.com', $reg_name."様");
$email->addReplyTo($reg_email, $reg_name."様");

也就是说,从您自己的地址发送,并使用提交者的地址作为回复。

更多代码please@AndreiTodorut,我编辑了问题,并发布了完整的代码,如果太长,很抱歉。请您也粘贴完整的错误消息,与我之前发布的完全相同:无效地址:电子邮件