简体   繁体   English

php mailer 添加确认邮件到输入的地址和表单提交

[英]php mailer add confirmation email to inputted address and form submit

I'm really trying to keep both functionality -- form fields filled out > form submit > goes to web masters email address with the information.我真的想保留这两个功能——填写表单字段> 表单提交> 转到带有信息的网站管理员电子邮件地址

I'm failing to add a second functionality;我没有添加第二个功能; I would like the user to be sent a custom confirmation message after form submit (all users get same default message) -- this message is emailed to the address inserted by user in 'email' field.我希望在表单提交后向用户发送自定义确认消息(所有用户都收到相同的默认消息) ——此消息通过电子邮件发送到用户在“电子邮件”字段中插入的地址。 I've gotten this;我得到了这个; but by doing so, I killed my original functionality;但是这样做,我杀死了我原来的功能; I would like to maintain both;我想同时保持两者; thus far I can get one to work, but then the other functionality dies -- currently it just sends confirmation to both emails below.到目前为止,我可以让一个工作,但随后另一个功能消失了——目前它只是向下面的两封电子邮件发送确认。

<?php

/* config start website@website.com*/

$emailAddress = 'website@gmail.com'; // form goes here
$to      = $_POST['email']; // user gets confirmation
$message = 'Thank you for submitting the form on our website.!';

/* config end */


require "phpmailer/class.phpmailer.php";

session_name("fancyform");
session_start();    


foreach($_POST as $k=>$v)
{
    if(ini_get('magic_quotes_gpc'))
    $_POST[$k]=stripslashes($_POST[$k]);

    $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}


$err = array();

if(!checkLen('name'))
    $err[]='The name field is too short or empty!';

if(!checkLen('email'))
    $err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
    $err[]='Your email is not valid!';

/* if(!checkLen('phone'))
    $err[]='The phone field is too short or empty!'; */

/*if(!checkLen('subject'))
    $err[]='You have not selected a subject!';*/

/* if(!checkLen('message'))
    $err[]='The message field is too short or empty!';*/

if((int)$_POST['captcha'] != $_SESSION['expect'])
    $err[]='The captcha code is wrong!';


if(count($err))
{
    if($_POST['ajax'])
    {
        echo '-1';
    }

    else if($_SERVER['HTTP_REFERER'])
    {
        $_SESSION['errStr'] = implode('<br />',$err);
        $_SESSION['post']=$_POST;

        header('Location: '.$_SERVER['HTTP_REFERER']);
    }

    exit;
}


$msg=
'Name:  '.$_POST['name'].'<br />
Phone:  '.$_POST['phone'].'<br />
Email:  '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Referred By:    '.$_POST['referer'].'<br /><br />
Message:<br /><br />

'.nl2br($_POST['message']).'

';


$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->addAddress($to);  // why can't both coexist?
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";

$mail->MsgHTML($msg);
$mail->msgHtml($message); // why can't both coexist?

$mail->Send();

// $to      = $_POST['email'];
// $message = 'Thank you for submitting the form on our website.!';


unset($_SESSION['post']);

if($_POST['ajax'])
{
    echo '1';
}
else
{
    $_SESSION['sent']=1;

    if($_SERVER['HTTP_REFERER'])
        header('Location: '.$_SERVER['HTTP_REFERER']);

    exit;
}

function checkLen($str,$len=2)
{
    return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
    return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}

?>

Code I added (which works, but overrides base form submit)我添加的代码(有效,但覆盖基本表单提交)

Grab email inputted in email field within online form > send confirmation there after submit.获取在在线表单中的电子邮件字段中输入的电子邮件 > 提交后在那里发送确认。

If I take the below lines out;如果我把下面的几行去掉; my form works normally;我的表格工作正常; user fills out form, sends to specified default email address - I just want to add in the confirmation message, and maintain the form working properly as well.用户填写表单,发送到指定的默认电子邮件地址 - 我只想添加确认消息,并保持表单正常工作。


$to      = $_POST['email']; // user gets confirmation
$message = 'Thank you for submitting the form on our website.!';


$mail->addAddress($to);  // why can't both coexist?

$mail->msgHtml($message); // why can't both coexist?

Maybe the question is how to allow multiple $mail->MsgHTML to send?也许问题是如何允许多个$mail->MsgHTML发送?

Original code;原始代码; I'm trying to add second functionality on to:我正在尝试将第二个功能添加到:

<?php

/* config start website@website.com*/

$emailAddress = 'website@gmail.com';

/* config end */


require "phpmailer/class.phpmailer.php";

session_name("fancyform");
session_start();    


foreach($_POST as $k=>$v)
{
    if(ini_get('magic_quotes_gpc'))
    $_POST[$k]=stripslashes($_POST[$k]);

    $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}


$err = array();

if(!checkLen('name'))
    $err[]='The name field is too short or empty!';

if(!checkLen('email'))
    $err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
    $err[]='Your email is not valid!';

/* if(!checkLen('phone'))
    $err[]='The phone field is too short or empty!'; */

/*if(!checkLen('subject'))
    $err[]='You have not selected a subject!';*/

/* if(!checkLen('message'))
    $err[]='The message field is too short or empty!';*/

if((int)$_POST['captcha'] != $_SESSION['expect'])
    $err[]='The captcha code is wrong!';


if(count($err))
{
    if($_POST['ajax'])
    {
        echo '-1';
    }

    else if($_SERVER['HTTP_REFERER'])
    {
        $_SESSION['errStr'] = implode('<br />',$err);
        $_SESSION['post']=$_POST;

        header('Location: '.$_SERVER['HTTP_REFERER']);
    }

    exit;
}


$msg=
'Name:  '.$_POST['name'].'<br />
Phone:  '.$_POST['phone'].'<br />
Email:  '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Referred By:    '.$_POST['referer'].'<br /><br />
Message:<br /><br />

'.nl2br($_POST['message']).'

';


$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";

$mail->MsgHTML($msg);

$mail->Send();


unset($_SESSION['post']);

if($_POST['ajax'])
{
    echo '1';
}
else
{
    $_SESSION['sent']=1;

    if($_SERVER['HTTP_REFERER'])
        header('Location: '.$_SERVER['HTTP_REFERER']);

    exit;
}

function checkLen($str,$len=2)
{
    return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
    return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}

?>

You can't send two mails with the same instance - you need to reassign $mail to a new instance of the PHPMailer class:你不能用同一个实例发送两封邮件——你需要将 $mail 重新分配给 PHPMailer 类的一个新实例:

$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);

$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";

$mail->MsgHtml($message);

$mail->Send();

///////////////////////////////////////////////////////////////
// then create new instance.. 
///////////////////////////////////////////////////////////////

$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($to);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";

$mail->MsgHtml($message); // why can't both coexist?

$mail->Send();

You can send multiple emails with the same instance with this code.您可以使用此代码使用同一实例发送多封电子邮件。 (Modified code from Amazon SES.) (来自 Amazon SES 的修改代码。)

$mail = new PHPMailer(true);

try {
    // Specify the SMTP settings.
    $mail->isSMTP();
    $mail->setFrom($sender, $senderName);
    $mail->Username   = $usernameSmtp;
    $mail->Password   = $passwordSmtp;
    $mail->Host       = $host;
    $mail->Port       = $port;
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = 'tls';
    $mail->addAddress($recipient); // Original content added here
    $mail->isHTML(true);
    $mail->Subject    = $subject;
    $mail->Body       = $bodyHtml;
    $mail->AltBody    = $bodyText;
    $mail->Send(); // The content of the form sent here
    echo "Order Submitted: ";
    $mail->ClearAddresses(); // Clear addresses
    $mail->addAddress($customerEmail); // Email address entered on form
    $mail->isHTML(true);
    $mail->Subject    = $confirmationSubject; // Custom subject
    $mail->Body       = $confirmationBodyHtml; // Custom reply
    $mail->AltBody    = $confirmationBodyText; // Text only custom reply
    $mail->Send(); // Send confirmation email
    echo "Confirmation Sent";
    //echo "Email sent!" , PHP_EOL;
} catch (phpmailerException $e) {
    echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
    echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM