I'm using swiftmailer to send emails. User is able to pick an attachment. The problem is that even with 1KB attachment, the script is running for more than 30 seconds which causes fatal error. I'm using XAMPP and smtp.gmail.com to send messages. tmp_path seems correct. Here's my code:
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>";
}