如果表单提交,则运行PHPMailer

时间:2016-02-02 21:02:25

标签: php forms phpmailer

我正在尝试仅在提交表单时运行PHPMailer。我尝试了几种不同的方法但没有成功。

我猜我的if(isset($ _ POST ['submit'])){tag是在错误的位置?我也不确定这段代码的哪一部分实际发送了电子邮件。当我使用

进行测试时
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
} 

它有效,所以我知道它运行正常。有任何想法只有在我的表单提交后才能运行吗?

<?php

    require_once 'C:\wamp\www\phpmailer\PHPMailer-master\PHPMailerAutoload.php';

    $mail = new PHPMailer;
    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host       = 'smtp.gmail.com   ';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                               // Enable SMTP authentication
    $mail->Username   = 'test@gmail.com';                 // SMTP username
    $mail->Password   = 'password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('tester@gmail.com', 'Marco');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = 'NEW APPLICATION';
    $mail->Body    = "--PERSONAL INFORMATION--
First Name: $fname Middle Name: $mname Last Name: $lname
Address:$address City:$city State:$state Zip:$zip ";

    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if (isset($_POST['submit']))
    {
$mail->send();
}
?>

1 个答案:

答案 0 :(得分:0)

确保您有

<input type="hidden" name="submit" value="foo" />

或将提交按钮更改为

<input type="submit" value="Submit!" name="submit" />

通过isset()检查

确保您知道表单的提交时间
相关问题