重组 php 联系表

Restructure php contact form

因为我已经对 PHP 一无所知,所以我需要一些编码方面的帮助。

我有 php 联系表单,它工作得很好,其中 html 表单元素正在使用 javascript 处理,这个 php 代码:

<?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,但我无法构建代码来发送与旧模板相同的带有表单元素的电子邮件模板。

<?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);
?>

非常感谢任何帮助。

您仍然可以使用旧程序的解析来构建主题和正文。然后您可以将它们插入 swift 消息中。请参阅下面的代码(您只需要更改您的 gmail 用户名和密码)。请注意,gmail 不喜欢服务器代表您发送邮件,您可能需要通过在此处的帐户上填写验证码来授权它: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);
?>

我认为@trex005 的回答很可靠 - 但您的悬赏消息表明它不够详细。因此,我只是添加了一个更详细的新答案。

SwiftMailer 的第一部分是定义您要用来发送邮件的服务器。此邮件属于 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 的类别。我已经使用 SendGrid 测试了我的示例,效果很好。但是,您的问题表明您想要使用 GMail。 您不能为此使用 GMail。

Google 假定发送到其 SMTP 服务器的任何电子邮件都是由对其进行身份验证的用户发送供个人使用。如果您尝试使用 GMail 发送带有 FROM: header 的电子邮件,FROM: header 将始终被 Google 更改为提供用户名的用户和密码。 Getting around this is not practical - 因为 GMail 不适用于此类电子邮件。

$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
  ;

上面的代码片段准确地显示了您需要如何使用 SMTP 服务器进行身份验证。

困难的部分已经解决了,所以让我们获取传递给这个邮件程序的所有变量:

$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]); 
}

这已在您的问题中提供并且正在运行,因此我们可以基本保持原样。 $headers 是多余的,因为电子邮件 headers 将由 SwiftMailler 生成,因此被删除。

现在,我们实际上需要使用 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

以上是根据您的要求发送电子邮件所需的所有关键任务 header。但是,如果您需要扩展它,还有更多选项 - 更多信息可以在 SwiftMailler docs.

中找到

最后,告诉邮件程序实际发送邮件:

$mailer->send($message);

总而言之,您的最终脚本看起来像这样,并且应该 copy/paste 替换您的旧脚本(您的 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 = "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]); 
}

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance()
  ->setSubject($subject)
  ->setFrom(array($from => $name))
  ->setTo($to)
  ->setBody($body);

$mailer->send($message);
?>