我有php联系表单,可以正常使用javascript和这个php代码处理html表单元素:
<?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);
?>
非常感谢任何帮助。
答案 0 :(得分:2)
您仍然可以使用旧程序中的解析来构建主题和正文。然后,您可以将它们插入快速消息中。请参阅下面的代码(您只需要更改您的Gmail用户名和密码)。请注意,gmail不喜欢代表您发送邮件的服务器,您可能需要在此处填写帐户中的验证码进行授权:https://accounts.google.com/displayunlockcaptcha
forEach(this.uploader._directives.over, this._removeOverClass, this);
答案 1 :(得分:0)
我认为@ trex005的答案是可靠的 - 但是你的赏金信息表明它不够详细。由于这个原因,我只是添加了一个更详细的新答案。
SwiftMailer的第一部分是定义您将用于发送邮件的服务器。此邮件属于transactional email类别 - 这意味着用户已为此电子邮件发送了一些内容。有两种服务对我这种邮件非常有用:SendGrid和Mandirll。我用SendGrid测试了我的例子,效果很好。但是,您的问题表明您想使用GMail。 您无法使用GMail。
Google假定发送到其SMTP服务器的任何电子邮件都是由正在验证它的用户发送给个人使用的。如果您尝试使用GMail发送包含FROM:
标题的电子邮件,则Google会始终将FROM:
标题更改为提供其用户名和密码的用户。 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
是多余的,因为电子邮件标题将由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
以上是根据您的要求发送电子邮件所需的所有关键任务标头。如果您需要扩展它,还有更多选项 - 可以在SwiftMailler docs中找到更多信息。
最后,告诉邮件程序实际发送邮件:
$mailer->send($message);
完成所有操作后,您的最终脚本将如下所示,并且应该是旧版本的复制/粘贴替换(除了您的用户名/密码)。
<?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);
?>