编辑:正如 Cheekysoft 在评论中指出的那样,编写的这段代码并不安全,并且会允许恶意用户通过您的应用发送任意电子邮件内容。
换句话说,不要按原样使用下面的代码。
我最终使用了 jQuery 和 PHP 的组合来完成工作。
- 在我的调查页面上,我添加了一个小表格来收集用户的电子邮件地址。
- 我使用jQuery将此表单和页面包装器标记的内容发布到 PHP 脚本。
- 在那个 PHP 脚本中,我使用Emogrifier将内联标签添加到 jQuery 传入的 HTML 中,并使用样式表作为参考(因为大多数电子邮件客户端,包括 Gmail,不允许使用链接样式表)。
- 然后(仍在 PHP 脚本中)我使用SwiftMailer(感谢 Marc B 将我指向它)和 Google Apps 帐户的 SMTP 功能发送了实际的电子邮件。
效果很好!
以防它将来对任何人有所帮助,这是 PHP 脚本(已清理):
<?php
/***** INITIALIZE *****/
/* Import libraries */
require_once 'swiftmailer/swift_required.php';
require_once 'emogrifier/emogrifier.php';
/* Email stylesheet location */
$stylesheet = 'http://example.com/email.css';
/* SMTP Account Info */
$smtpusername = "sender@gmail.com";
$smtppassword = "senderpassword";
$smtpserver = "smtp.gmail.com";
$smtpport = 465;
$smtpsecurity = "ssl";
/***** RETRIEVE THE DATA *****/
/* Retrieve the passed-in variables */
/* HTML for the email body */
$html = $_POST['content'];
/* Recipient mail address */
$address = $_POST['address'];
/* Recipient name */
$name = $_POST['name'];
if ($name==NULL || $name=="") {
$name = "You";
}
/***** MODIFY THE HTML *****/
// Get stylesheet contents as a string
$css = file_get_contents($stylesheet);
// Convert stylesheet into in-line styles using Emogrifier - http://www.pelagodesign.com/sidecar/emogrifier/
$converter = new Emogrifier($html, $css);
$content = $converter->emogrify();
/***** CREATE THE MESSAGE *****/
/* Create the message */
$message = Swift_Message::newInstance()
//Give the message a subject
->setSubject("Results for $name")
//Set the From address with an associative array
->setFrom(array('sender@gmail.com' => 'Sender Name'))
//Set the To addresses with an associative array
->setTo(array($address => $name))
//Give it a body
->setBody($content, 'text/html')
;
/***** SEND THE EMAIL *****/
//Create the Transport
$transport = Swift_SmtpTransport::newInstance($smtpserver, $smtpport, $smtpsecurity)
->setUsername($smtpusername)
->setPassword($smtppassword)
;
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
if ($result == "1") {
echo "<span class='sendstatus success'>Email sent successfully. </span>";
} else {
echo "<span class='sendstatus failure'>An error occurred. Result code: $result </span>";
}
?>
这是 jQuery 表单(稍微简化了一点):
<div id="emailresults">
<form id="emailRecsForm" action="http://example.com/emailresults/emailrecs.php"> <!-- THIS PAGE WHERE THE PHP ABOVE IS LOCATED -->
<h3><img src="email-icon.png" /> Email Your Results</h3>
<label for="name">Name</label><input type="text" id="name" name="name" placeholder="Recipient's name (optional)" />
<label for="address">Email address</label><input type="text" id="address" name="address" class="required email" placeholder="recipient@address.org" />
<div id="submitdiv"><input type="submit" class="submit" value="Send Results" /></div>
</form>
<!-- the result of the send will be rendered inside this div -->
<div id="result"></div>
</div>
<script>
/* attach a submit handler to the form */
$("#emailRecsForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
$( "#submitdiv" ).empty().append('<span class="sendstatus working"><img src="/images/loadwheel.gif" alt="Sending..."></img></span>');
/* get some values from elements on the page: */
var $form = $( this ),
name = $form.find( 'input[name="name"]' ).val(),
address = $form.find( 'input[name="address"]' ).val(),
html = $('.container').html(),
url = "http://example.com/emailrecs.php";
/* Send the data using post and put the results in a div */
$.post( url, { name: name, address: address, content: html },
function( data ) {
$( "#submitdiv" ).empty().append('<br />').append( data ).append('<input type="submit" class="submit" value="Send Results" />');
$form.find( 'input[name="name"]' ).val("");
$form.find( 'input[name="address"]' ).val("");
}
);
});
</script>