我使用phpmailer通过电子邮件发送表单。它可以工作,但只能使用长度为7或更长的电子邮件用户名。任何更少的东西,我得到错误"无法实例化邮件功能。"我使用的是gmail的smtp服务器,我知道Google只允许使用长度为> = 6的帐号,所以我不知道这是否会干扰?这是php文件。
require_once("../PHPMailer/class.phpmailer.php");
require_once('../PHPMailer/PHPMailerAutoload.php');
//Create a new PHPMailer instance
$mail = new PHPMailer;
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$fullname = $firstname . ' ' . $lastname;
$email = $_POST["email"];
$subject = $_POST["subject"];
$filename = basename($_FILES['resume']['name']);
$filetype = substr($filename,
strrpos($filename, '.') + 1);
$filesize = $_FILES["resume"]["size"]/2048;
//Honeypot used to catch spam bots
if ($_POST["address"] != "") {
echo "SPAM HONEYPOT";
exit;
}
//Checks for injection
$email = preg_replace("([\r\n])", "", $email);
$find = "/(content-type|bcc:|cc:)/i";
if (preg_match($find, $fullname) || preg_match($find, $email) || preg_match($find, $url) || preg_match($find, $subject)) {
echo "<h1>Error</h1>\n
<p>No meta/header injections, please.</p>";
exit;
}
//Test for valid inputs
if(!preg_match("/^[a-zA-Z ]*$/", $firstname) || (empty($firstname))){
echo 'Invalid first name.';
exit;
}
if(!preg_match("/^[a-zA-Z ]*$/", $lastname) || (empty($lastname))){
echo 'Invalid last name.';
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo 'Invalid email format.';
exit;
}
if($filesize > 5){
echo 'Invalid file size.';
exit;
}
$mail->Host = 'smtp.gmail.com';
//Set who the message is to be sent from
$mail->setFrom($email, 'Online Application');
//Set who the message is to be sent to
$mail->addAddress('joe@sample.org', 'Joe Smith');
//Set the subject line
$mail->Subject = $subject;
$jobmessage = 'Joe, </br> You have an applicant for the position' . ' ' . $subject . ' ' . ' whose name is' . ' ' . $fullname . ' ' . 'and email is' . ' ' . $email . '.</br></br>' . 'This is an automated message.';
$mail->msgHTML($jobmessage);
//Attach an image file
$mail->addAttachment($_FILES['uploaded_file']);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Application sent!";
}
`