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

时间:2018-07-12 05:58:09

标签: php phpmailer

我在使用表单时遇到麻烦,当未插入用户邮件时,它总是返回错误消息“无效地址:电子邮件”。我知道$ 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 个答案:

答案 0 :(得分:1)

这是因为您正在这样做:

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

电子邮件一词,后跟一个换行符,后跟一个电子邮件地址本身并不是有效的电子邮件地址,因此会因您看到的错误而被拒绝。您还将追加一个尚未定义的变量。改为这样做:

$reg_email = $_POST['f_mail'];

我对此也感到困惑:

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

PHPMailer使用addAddress,而不是addTo,因此除非这是您的JPHPmailer子类中的新方法或别名,否则是错误的。

也不要这样做:

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

这是伪造的,将导致您的邮件被收件人退回或过滤垃圾邮件。改为这样做:

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

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