php - 使用所见即所得的文本编辑器创建电子邮件
问题描述
我已经创建了我网站的管理部分,其中一部分将用于向订阅它们的人发送新闻通讯。将要执行此操作的人没有编码经验,这就是为什么不能仅使用 php/html 创建电子邮件的原因。
我试图做的是使用所见即所得的文本编辑器让他们更容易做到这一点。我为此使用了tinymce。但是,我在添加图像时遇到问题,如果图像已上传,它们根本不会显示在 Outlook 或 gmail 中。这不是很好,因为这意味着上传图像的唯一方法是从网站复制图像地址并以这种方式添加它们。另一个问题是,虽然在编辑器上您可以调整图像大小,但一旦发送电子邮件,原始图像大小就是发送的内容。我试图通过执行以下操作来使其正常工作:
<script>
$(document).ready(function() {
tinymce.init({
selector: "textarea",
convert_urls: false,
relative_urls: false,
remove_script_host: false,
fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt",
theme: "modern",
paste_data_images: false,
plugins: [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"emoticons template paste textcolor colorpicker textpattern"
],
toolbar1: "sizeselect | fontselect | fontsizeselect |insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | editimage | imageoptions",
toolbar2: "print preview media | forecolor backcolor emoticons",
image_advtab: true,
file_picker_callback: function(callback, value, meta) {
if (meta.filetype == 'image') {
$('#upload').trigger('click');
$('#upload').on('change', function() {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(e) {
callback(e.target.result, {
alt: ''
});
};
reader.readAsDataURL(file);
});
}
},
templates: [{
title: 'Test template 1',
content: 'Test 1'
}, {
title: 'Test template 2',
content: 'Test 2'
}]
});
});
</script>
<style>
.hidden{display:none;}
</style>
</head>
<body>
<div id="wrapper">
<?php include('menu.php');?>
<p><a href="./">JG Ross Admin Index</a></p>
<h2>Send Email</h2>
<p>(Both subject and content must be filled out in order to send the email)</p>
<?php
//if form has been submitted process it
if(isset($_POST['submit'])){
$_POST = array_map( 'stripslashes', $_POST );
//collect form data
extract($_POST);
//very basic validation
if($subject ==''){
$error[] = 'Please enter the title.';
}
if($emailCont ==''){
$error[] = 'Please enter the content.';
}
if(!isset($error)){
}
}
//check for any errors
if(isset($error)){
foreach($error as $error){
echo '<p class="error">'.$error.'</p>';
}
}
?>
<form action='createEmail.php' method='post'>
<div id="toolbar-container"></div>
<p><label>Subject</label><br />
<input type='text' name='subject' required value='<?php if(isset($error)){ echo $_POST['subject'];}?>'></p>
<p><label>Content</label><br />
<textarea name='emailCont' id="emailCont" cols='60' rows='10'><?php if(isset($error)){ echo $_POST['emailCont'];}?></textarea></p>
<input name="image" type="file" id="upload" class="hidden" onchange="">
<p><input type='submit' name='submit' value='Submit'></p>
</form>
</div>
</body>
</html>
发送邮件的页面如下:
<?php
// Starting session
require_once('../includes/config.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'C:\PHPMailer\PHPMailer-master\src\Exception.php';
require 'C:\PHPMailer\PHPMailer-master\src\PHPMailer.php';
require 'C:\PHPMailer\PHPMailer-master\src\SMTP.php';
$subject = $_POST['subject'];
$body = $_POST['emailCont'];
$sql = "SELECT email FROM subscribers";
foreach ($db->query($sql) as $row) {
$message = "<html><body>";
$message .= "<table width='100%' bgcolor='#e0e0e0' cellpadding='0' cellspacing='0' border='0'>";
$message .= "<tr><td>";
$message .= "<table align='center' width='100%' border='0' cellpadding='0' cellspacing='0' style='max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'>";
$message .= "<thead>
<tr height='80'>
<th colspan='4' style='background-color:#f5f5f5; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#000000; font-size: 27px;' >Newsletter</th>
</tr>
</thead>";
$message .= "<tbody>
<tr height='80'>
<td colspan='4' align='center' style='background-color:#f5f5f5;'>
<img scr = $body>
</td>
</tr>
<tr height='20'>
<td colspan='4' align='center' style='background-color:#f5f5f5;'>
<p 'font-size:8px;'><a href='http://localhost/jgross/unsubscribe.php'>Unsubscribe</a></p>
</td>
</tr>
</tbody>";
$message .= "</table>";
$message .= "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$mail = new PHPMailer(TRUE);
try {
$mail->isHTML(true);
$mail->SetFrom('donotreply@mydomain.com', $subject);
$mail->AddAddress($row['email']);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = '********';
$mail->Password = '********';
$mail->Port = 587;
if ($mail->addReplyTo('********')) {
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
//Send the message, check for errors
if (!$mail->send()) {
//The reason for failing to send will be in $mail->ErrorInfo
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'The Newsletter was sucessfully sent<br>';
header("Refresh:3; url=index.php");
echo "The Newsletter was sucessfully sent<br>";
}
} else {
$msg = 'Invalid email address, message ignored.';
}
}catch (Exception $e)
{
echo $e->errorMessage();
$mail->smtp->reset();
}
$mail->clearAddresses();
}
当我在更新后查看图像源时,它类似于:
斑点:http://localhost/d27a27cd-32a3-443e-ad7c-f3bf745fa500
这可能是个问题吗?
有没有办法让它工作,以便图像通过并以我调整它们的大小?还是我在浪费时间尝试这样做?如果是这样,将新闻通讯发送给订阅者的更好方法是什么?
解决方案
如果其他人有这个问题并且不像我一样不知道,有一个外部插件可以添加到tinymce,称为响应文件管理器,可以在这里找到:https ://www.responsivefilemanager.com/ 。添加后,您可以存储图像并可以通过电子邮件发送图像而不会出现任何问题。
注意:如果通过本地主机完成,图像将不会显示在电子邮件中,需要通过适当的网站完成,例如。我正在使用 000webhost。
它也不适用于最新版本的tinymce(据我所知),尝试过但没有用。所以,我使用的是 4.3.3 版本,它可以正常工作。
希望这可以帮助某人