我正在使用swiftmailer发送电子邮件。用户可以选择附件。问题是即使使用1KB附件,脚本也会运行超过30秒,这会导致致命错误。 我正在使用XAMPP和smtp.gmail.com发送消息。 tmp_path似乎正确。 这是我的代码:
require_once '../mailer/lib/swift_required.php';
sendMessage();
function validate($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function sendMessage()
{
$SEND_MAIL_TO = "jaroslawwatroba@wp.pl";
$MAILER_FROM = "jarekwatroba@gmail.com";
$MAILER_USERNAME = "jarekwatroba@gmail.com";
$MAILER_PASSWORD = "***";
$MAILER_SMTP = "smtp.gmail.com";
$MAILER_SMTP_PORT = 465;
$MAILER_SMTP_USE_SSL = true;
if (!isset($_POST["sent"]))
return;
$name = validate($_POST["name_text"]);
$email = validate($_POST["email_text"]);
$phone = validate($_POST["phone_text"]);
$interest = validate($_POST["interest_text"]);
$language = validate($_POST["language_text"]);
$message = validate($_POST["message_text"]);
if (empty($name) || empty($email))
{
echo "<p><strong style=\"color: red\">Please fill in the required fields!</strong></p>";
return;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "<p><strong style=\"color: red\">Please fill in a correct email address!</strong></p>";
return;
}
$message = Swift_Message::newInstance()
->setSubject("Contact by form: ".$name." ".$interest." ".$language)
->setFrom(array($MAILER_FROM))
->setTo(array($SEND_MAIL_TO))
->setBody($message);
echo "Path: ".$_FILES["attachment_text"]["tmp_name"];
$attachement = Swift_Attachment::fromPath($_FILES["attachment_text"]["tmp_name"], $_FILES["attachment_text"]["type"]);
$attachement->setFilename($_FILES["attachment_text"]["name"]);
$message->attach($attachement);
if (!$MAILER_SMTP_USE_SSL)
$transport = Swift_SmtpTransport::newInstance($MAILER_SMTP, $MAILER_SMTP_PORT);
else
$transport = Swift_SmtpTransport::newInstance($MAILER_SMTP, $MAILER_SMTP_PORT, 'ssl');
$transport->setUsername($MAILER_USERNAME);
$transport->setPassword($MAILER_PASSWORD);
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
if ($result == 1)
echo "<p><strong style=\"color: green\">Your message is sent!</strong></p>";
else
echo "<p><strong style=\"color: green\">Your message wasn't successfully sent...</strong></p>";
}