Restructure php contact form
Asked Answered
H

2

2

since I am already ignorant with PHP I need help in some coding.

I have php contact form that work just fine where html form elements is being processed using javascript and this php code:

<?php

    $to = "[email protected]";
    $from = $_REQUEST['email'];
    $name = $_REQUEST['name'];
    $headers = "From: $from";
    $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]); }

    $send = mail($to, $subject, $body, $headers);

?>

Since I want to handle sending mail using SMTP server instead of mail() through local host, I used SwiftMailer except I can not structure code to send the same email template with form elements as old one.

<?php

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('[email protected]')
  ->setPassword('password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('[Contact form] You have a message from')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]' => 'A name'))
  ->setBody('Here is the message itself');

// Send the message
$result = $mailer->send($message);
?>

Any help is much appreciated.

Heisel answered 31/7, 2015 at 13:10 Comment(2)
I don't really unterstand the question. Using swiftmailer instead of mail usually just makes you use the variables you already have in a slightly different context and environments. Where exactly are you stuck?Teenyweeny
@Teenyweeny Building the same message subject, body used in old oneHeisel
N
2

You can still use the parsing from your old program to build the subject and body. You can then just plug them into the swift message. See code below (You should only need to change your gmail username and password). Please note, gmail does not like servers sending mail on your behalf, and you may need to authorize it by filling out a captcha on the account here : https://accounts.google.com/displayunlockcaptcha

<?php
$gmail_username = '[email protected]';
$gmail_password = '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]); }

require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername($gmail_username)
  ->setPassword($gmail_password)
  ;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance($subject)
  ->setFrom($gmail_username)
  ->setReplyTo(array($from => $name))
  ->setTo($to)
  ->setBody($body);
// Send the message
$result = $mailer->send($message);
?>
Narcisanarcissism answered 4/8, 2015 at 20:43 Comment(0)
T
0

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);
?>
Therapeutics answered 5/8, 2015 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.