重组php联系表 [英] Restructure php contact form

查看:91
本文介绍了重组php联系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我已经不了解PHP,所以我需要帮助。

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

我有php联系人表单,使用javascript处理html表单元素和这个php代码:

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

<?php

    $to = "contact@ihabovich.ga";
    $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);

?>

由于我想通过本地主机处理使用SMTP服务器发送邮件而不是mail(),我使用SwiftMailer除了我不能构造代码发送与旧的表单元素相同的电子邮件模板。

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('username@gmail.com')
  ->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('john@doe.com' => 'John Doe'))
  ->setTo(array('username@gmail.com' => 'A name'))
  ->setBody('Here is the message itself');

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

非常感谢任何帮助。

推荐答案

您仍然可以使用旧程序的解析来构建主题和正文。然后,您可以将其插入到快速消息中。请参阅下面的代码(您只需要更改您的Gmail用户名和密码)。请注意,gmail不喜欢代表您发送邮件的服务器,您可能需要通过在此处的帐户填写验证码进行授权: https://accounts.google.com/displayunlockcaptcha

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 = 'username@gmail.com';
$gmail_password = 'password';
$to = "contact@ihabovich.ga";
$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);
?>

这篇关于重组php联系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆