I think @trex005's answer was solid - but your bounty message suggests it was not detailed enough. I am only adding a new answer with more detail because of that.
the first piece of SwiftMailer is defining what server you are going to use to send out the mail. This mail falls under the category of transactional email - which means that a user has done something for this email to be sent. Two services stand out to me as being good with this kind of mail: SendGrid and Mandirll. I've tested my example with SendGrid and it works great. However, your question suggests that you would like to use GMail. You cannot use GMail for this.
Google assumes that any email being sent to it's SMTP server is being sent for personal uses by the user that is authenticating it. If you try to send an email with a FROM:
header using GMail, the FROM:
header will always be changed by Google to the user that provided their username and password. Getting around this is not practical - because GMail is not intended for this kind of email.
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587, 'tls')
->setUsername('Sendgrid/Mandrill User') //Replace this with your info
->setPassword('Sendgrid/Mandrill Password') //Replace this with your info
;
The above code snipper shows exactly how you need authenticate using an SMTP server.
The hard part is out of the way, so lets grab all the variables being passed to this mailer:
$to = "[email protected]";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = "[Contact form] You have a message from $name.";
$fields = array();
$fields{"name"} = "Name";
$fields{"email"} = "Email";
$fields{"phone"} = "Phone";
$fields{"department"} = "Department";
$fields{"message"} = "Message";
$body = "Here is what was sent:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
This was provided in your question and was working, so we can leave it mostly as is. $headers
is redundant, as the email headers will be generated by SwiftMailler so that is removed.
Now, we actually need to build the message using SwiftMailler:
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject) //User provided subject
->setFrom(array($from => $name)) //The user
->setTo($to) //This should be you
->setBody($body); //whatever the user wanted to say
Above are all the mission critical headers that are needed to send the email per your requirements. There are more options if you ever need to expand it however - more information can be found within the SwiftMailler docs.
Finally, tell the mailer to actually send the message:
$mailer->send($message);
When all is said and done, your final script looks like this, and should be a copy/paste replacement for your old one (with the exception of your Username/password).
<?php
require_once 'lib/swift_required.php';
//As previously mentioned, you may also use Mandrill for this.
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587, 'tls')
->setUsername(/*Your SendGrid Username*/)
->setPassword(/*Your SendGrid Password*/)
;
$to = "[email protected]";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = "[Contact form] You have a message from $name.";
$fields = array();
$fields{"name"} = "Name";
$fields{"email"} = "Email";
$fields{"phone"} = "Phone";
$fields{"department"} = "Department";
$fields{"message"} = "Message";
$body = "Here is what was sent:\n\n";
foreach($fields as $a => $b){
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom(array($from => $name))
->setTo($to)
->setBody($body);
$mailer->send($message);
?>