php邮件发送邮件碎片

时间:2015-02-03 15:33:41

标签: php email

使用php邮件发送邮件,但邮件收到3部分,因为我要求3件名称电子邮件和查询。我在3个不同的时间在邮件中收到这3个信息,甚至在收到写电子邮件后格式化获取无效格式的错误消息。请检查并尝试解决它。 所以你能帮助我知道,我错了吗?请解决。

<?php

$name = $email = $query = "";
$name_err = $email_err = $query_err = "";

if ($_SERVER ["REQUEST_METHOD"] == "POST") {

    if (empty ( $_POST ["name"] )) {
        $name_err = 'Please Enter Your Name';
    } else {
        $name = sendmail ( $_POST ["name"] );
    }
    if (empty ( $_POST ["email"] )) {
        $email_err = 'Please Enter Your Email Id';
    } else {
        $email = sendmail ( $_POST ["email"] );

        if (!filter_var ( $email, FILTER_VALIDATE_EMAIL )) {
            $email_err = "Invalid email format";
        }
    }
    if (empty ( $_POST ["query"] )) {
        $query_err = 'Please type your query';
    } else {
        $query = sendmail ( $_POST ["query"] );
    }
}

function validate_email($data) {
    $data = trim ( $data );
    $data = stripslashes ( $data );
    $data = htmlspecialchars ( $data );

    return $data;
    $message = $data;
}


function sendmail($message) {

    //require_once ("PHPMailer/class.phpmailer.php");
    //require_once ("PHPMailer/class.smtp.php");

    require_once ("PHPMailer/PHPMailerAutoload.php");

    $subject = "Visitor Query";
    $from = "007shiva007@gmail.com"; // example a@b.com
    $password = 'password';

    $mail = new PHPMailer();
    $body = $message;
    $mail->IsSMTP();
    $mail->SMTPAuth = true; // turn on SMTP authentication
    //$mail->SMTPDebug = 1;

    $mail->Host = 'smtp.gmail.com'; // sets SMTP server
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->Username = $from; // SMTP username
    $mail->Password = $password; // SMTP password

    $mail->AddAddress ( "partyushgoyal@gmail.com" );

    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->From = $from;
    $mail->ContentType = "text/html";

    $result = $mail->Send();

}
?>

1 个答案:

答案 0 :(得分:0)

以这种方式试试。首先准备你的消息,如果一切都填满(没有错误),发送电子邮件。

$message = '';
$error = array();

if ($_SERVER ["REQUEST_METHOD"] == "POST") {
    if (empty ( $_POST ["name"] )) {
        $error[] = 'Please Enter Your Name';
    } else {
        $message = 'Name: ' . $_POST["name"] . '<br>';
    }
    if (empty ( $_POST ["email"] )) {
        $error[] = 'Please Enter Your Email Id';
    } else {
        if (!filter_var ( $_POST['email'], FILTER_VALIDATE_EMAIL )) {
            $error[] = "Invalid email format";
        } else {
            $message .= 'e-mail: ' .$_POST["email"] . '<br>';
        }
    }
    if (empty ( $_POST ["query"] )) {
        $error[] = 'Please type your query';
    } else {
        $message .= 'message: ' . $_POST["query"];
    }
}

if (count($error == 0)) {
    // SEND MAIL
} else {
    // show error messages
}