代码之家  ›  专栏  ›  技术社区  ›  Fernando Souza

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

  •  -1
  • Fernando Souza  · 技术社区  · 6 年前

    我的表单有问题,当用户邮件没有插入时,它总是返回错误消息“无效地址:电子邮件”。我知道$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
    
    
    
    
    ?>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Synchro    6 年前

    $reg_email .= "email"."\r\n" . $_POST['f_mail'];
    

    $reg_email = $_POST['f_mail'];
    

    $email->addTo($aMailto[$i]);
    

    addAddress addTo JPHPmailer

    $email->setFrom($reg_email, $reg_name."様");
    

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