我在一个系统上编码,管理员应该能够点击按钮并向所有用户发送邮件。当我单击一个href并使用type = 2重定向时,它会将邮件发送到所有帐户,但是应该是动态的内容只返回数组中最后一个用户的电子邮件。
<?php
include "core/init.php";
error_reporting(E_ALL);
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtprotocl';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "mymail@mail.dk";
$mail->Password = "password";
$mail->CharSet = 'UTF-8';
$mail->setFrom('mymail@mail.dk', 'nameofmailholder');
if ($_GET["type"]) {
if ($_GET["type"] == 2){
$confirmede = 0;
$getVendorc = $db->prepare("SELECT * FROM db");
$getVendorc->execute();
$vresultc = $getVendorc->get_result();
$getVendorc->store_result();
while ($rowing = $vresultc->fetch_assoc()) {
$removeSpace = explode(" ", decrypt($rowing["email"]));
$Uemail = implode("+", $removeSpace);
$mail->addReplyTo($Uemail);
$mail->addAddress($Uemail);
}
$mail->isHTML(true);
$mail->Subject = 'welcome';
$mail->Body = '<a href="website?id='. encrypt($Uemail) .'" target="_blank"><button >Bekræft email</button></a>';
} else {
urlLocation("index");
}
}
$mail->AltBody = 'aa';
if($mail->send()) {
urlLocation("../../index");
}?>
答案 0 :(得分:1)
创建电子邮件(使用PHPMailer功能)并将其发送到循环内(这是您应该做的),或者如果您想一次向所有收件人发送一封电子邮件(不向任何人透露电子邮件,请执行以下操作),执行以下操作:因为它是BCC)。我不推荐这个,但我想它可以工作。
$mail->addAddress("fake@address.com");
$mail->addBCC($email1, $email2, $email3, $email4);
这应该适用于在一封电子邮件中向多个收件人发送相同的电子邮件,而不会向所有人透露所有电子邮件。
就个人而言,我使用wuMail ...它使得使用PHPMailer变得更加容易,特别是使用SMTP。这是我的设置,登录详细信息明显改变了......我将在下面解释它是如何工作的,但你需要下载wuMail(底部链接),以便config.php&amp ;;正确引用文件(对于PHPMailer)。 wuMail.php ...下面的代码块是引用,但不能在没有其他wuMail下载的情况下单独使用。
首先发生的是我们设置系统默认值...这些用于某些电子邮件参数未提供给wuMail()的情况。这是一个故障安全,如果你想要一个默认的地址,以防系统的一部分尝试使用wuMail()而不提供地址..这将让你快速发现这些类型的错误。
以下是config.php中的代码...您需要在任何要使用wuMail()函数的文件中的config.php(发送邮件)。配置将提前设置&amp;然后引入wuMail.php文件本身。所以不要包含wuMail.php文件,只需包含config.php!
<?php
/*
NOTE: These settings have been prepared by
WUBUR.COM / WUBUR LLC / BILLY LOWERY / WILL PASSMORE
*/
# DO NOT REMOVE:
$wuMail_Config = new stdClass();
//SENDER CONFIG SETTINGS - YOU CAN CHANGE THESE:
// ----> Note: This current configuration has a form sending to the same address each time
// ----> This could obviously be adapted to allow for emails being sent elsewhere
$wuMail_Config->SiteName = 'Wubur.com';
//SERVER CONFIG SETTINGS - YOU CAN CHANGE THESE:
$wuMail_Config->isSMTP = true; // Set mailer to use SMTP (TRUE / FALSE)
$wuMail_Config->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$wuMail_Config->SMTPAuth = true; // Enable SMTP authentication (TRUE / FALSE)
$wuMail_Config->AuthType = 'LOGIN'; // Authentification type... ex: PLAIN, LOGIN
$wuMail_Config->Username = 'USERNAME@gmail.com'; // Only blank for GoDaddy's servers
$wuMail_Config->Password = 'PASSWORD'; // Only blank for GoDaddy's servers
$wuMail_Config->SMTPSecure = 'ssl'; // ('tls', 'ssl' or false)
$wuMail_Config->Port = 465; // TCP port to connect to
$wuMail_Config->Exceptions = true; // true/false, if Exceptions enabled
$wuMail_Config->SMTPDebug = 0; // Enable verbose debug output ~~ must be 0 in production environment
//MESSAGE CONFIG SETTINGS - YOU CAN CHANGE THESE:
$wuMail_Config->DefaultToAddress = 'to@email.com';
$wuMail_Config->DefaultToName = 'To Name';
$wuMail_Config->DefaultCC = false; // no default CC
$wuMail_Config->DefaultBCC = false; // no default BCC
$wuMail_Config->DefaultFromAddress = 'from@email.com';
$wuMail_Config->DefaultFromName = 'From Name';
$wuMail_Config->DefaultReplyAddress = 'replyaddress@email.com';
$wuMail_Config->DefaultReplyName = 'Reply Name';
$wuMail_Config->DefaultSubject = 'Default Subject';
# MESSAGE / HTML VERSION CONFIG SETTINGS - YOU CAN CHANGE THESE. BE CAREFUL:
$wuMail_Config->DefaultMessage = 'Default Message (Message Not Supplied)';
$wuMail_Config->ForceHTML = true; // (recommended: true)
// If set to TRUE, and no HTML version of message is supplied to wuMail function, use the HTML template below...Otherwise use HTML supplied to wuMail function if it is supplied.
// If set to FALSE, and no HTML version of message is supplied to wuMail function, simply display a non-HTML message to recipient. If HTML version is supplied, HTML version will be used instead of template
# DefaultHTML: Simply use {!messageInsert!} to insert the regular non-HTML message into the template. {!siteNameInsert!} will insert the config site name.
$wuMail_Config->DefaultHTML = '
<div>
<center><a href="site.com"><img style="width:350px; height:auto; margin-bottom:25px;" src="site.com/logo.png" alt="Site Name" /></a></center>
<div style="width:100%; color:white; background-color:#c02026; font-weight:500; padding:10px;">Important Information</div>
<div style="width:100%; padding:25px; color:black; background-color:#f2f2f2;">
{!messageInsert!}
</div>
</div>
';
# NOTE: The 'HTML' key in the options data array for wuMail can be a template with {!messageInsert!} or {!siteNameInsert!} as variables!
// PHPMailer Path Settings:
$wuMail_Path = ""; // path from root dir for site access
// DO NOT REMOVE:
require $wuMail_Path . "wuMail.php";
?>
网址&amp;显然,模板中的徽标图像应该更新...如果有人提供没有HTML版本的电子邮件,这是默认模板。这样每个电子邮件都可以在允许的情况下使用HTML模板,即使开发人员在第一时间没有正确使用HTML模板:)获胜的系统设置!
接下来我们有wuMail.php本身......
<?php
/*
NOTE: These settings have been prepared by
WUBUR.COM / WUBUR LLC / BILLY LOWERY / WILL PASSMORE
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require $wuMail_Path . 'src/Exception.php';
require $wuMail_Path . 'src/PHPMailer.php';
require $wuMail_Path . 'src/SMTP.php';
function wuMail($data, $multi = false){
global $wuMail_Config;
if(!is_array($data)){
$useDefaults = true;
} else {
$useDefaults = false;
}
$mailSettings = array(
"TOADDRESS" => $wuMail_Config->DefaultToAddress,
"TONAME" => $wuMail_Config->DefaultToName,
"CC" => $wuMail_Config->DefaultCC,
"BCC" => $wuMail_Config->DefaultBCC,
"SUBJECT" => $wuMail_Config->DefaultSubject,
"MESSAGE" => $wuMail_Config->DefaultMessage,
"HTML" => $wuMail_Config->DefaultHTML,
"FROMADDRESS" => $wuMail_Config->DefaultFromAddress,
"FROMNAME" => $wuMail_Config->DefaultFromName,
"REPLYADDRESS" => $wuMail_Config->DefaultReplyAddress,
"REPLYNAME" => $wuMail_Config->DefaultReplyName
);
# NOTES: THE FOLLOWING CAN BE ARRAYS: ToAddress, ToName (arr key must match ToAddress arr), CC, BCC
# IN FUTURE, YOU CAN LINE UP EMAILS TO SEND SEPARATELY BY SIMPLY MAKING MULTIDIMENSIONAL ARRAY OF DATA, AND SETTING PARAM 2 (MULTI) EQUAL TO TRUE IN WUMAIL
if($useDefaults !== true){
$submittedOpts = array();
foreach($data as $submittedOption => $submittedValue){
$submittedOptionUPPER = strtoupper($submittedOption);
$submittedOpts[] = $submittedOptionUPPER;
if(isset($mailSettings[$submittedOptionUPPER])){
$mailSettings[$submittedOptionUPPER] = $data[$submittedOption];
} else {
echo "ERROR: SUBMITTED MAIL OPTION NOT ACCEPTED";
}
}
if(($mailSettings['TOADDRESS'] !== $wuMail_Config->DefaultToAddress) && !in_array('TONAME', $submittedOpts)){ # To address supplied, but no to name supplied
# do not use a toName...
$mailSettings['TONAME'] = false;
}
if(($mailSettings['FROMADDRESS'] !== $wuMail_Config->DefaultFromAddress) && !in_array('FROMNAME', $submittedOpts)){ # From address supplied, but no fromname supplied
$mailSettings['FROMNAME'] = false;
# do not use fromname below, because the supplied from address differs from the default in settings
}
if(($mailSettings['REPLYADDRESS'] !== $wuMail_Config->DefaultFromAddress) && !in_array('REPLYNAME', $submittedOpts)){ # Reply address supplied, but no replyname supplied
$mailSettings['REPLYNAME'] = false;
# do not use replyname below, because the supplied reply address differs from the default in settings
}
} # useDefaults !== true
$mail = new PHPMailer($wuMail_Config->Exceptions);
try {
//Server Settings (from PHPMailer/config.php) - Change in config.php file, not here!
$mail->SMTPDebug = $wuMail_Config->SMTPDebug;
if($wuMail_Config->isSMTP === true){ $mail->isSMTP(); }
$mail->Host = $wuMail_Config->Host;
$mail->SMTPAuth = $wuMail_Config->SMTPAuth;
$mail->AuthType = $wuMail_Config->AuthType;
$mail->Username = $wuMail_Config->Username;
$mail->Password = $wuMail_Config->Password;
$mail->SMTPSecure = $wuMail_Config->SMTPSecure;
$mail->Port = $wuMail_Config->Port;
//Recipients
if($mailSettings['FROMNAME'] !== false){
$mail->setFrom($mailSettings['FROMADDRESS'], $mailSettings['FROMNAME']);
} else {
$mail->setFrom($mailSettings['FROMADDRESS']);
}
if($mailSettings['TONAME'] !== false){
$mail->addAddress($mailSettings['TOADDRESS'], $mailSettings['TONAME']);
} else {
$mail->addAddress($mailSettings['TOADDRESS']);
}
if($mailSettings['REPLYNAME'] !== false){
$mail->addReplyTo($mailSettings['REPLYADDRESS'], $mailSettings['REPLYNAME']);
} else {
$mail->addReplyTo($mailSettings['REPLYADDRESS']);
}
if($mailSettings['REPLYNAME'] !== false){
$mail->addReplyTo($mailSettings['REPLYADDRESS'], $mailSettings['REPLYNAME']);
} else {
$mail->addReplyTo($mailSettings['REPLYADDRESS']);
}
if($mailSettings['CC'] !== false){
$mail->addCC($mailSettings['CC']);
}
if($mailSettings['BCC'] !== false){
$mail->addCC($mailSettings['BCC']);
}
//Attachments
#$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
#$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
if($wuMail_Config->ForceHTML !== true && !in_array("HTML", $submittedOpts)){ # ForceHTML is not enforced, and HTML not submitted.... Use plain text message:
$mailSettings['HTML'] = $mailSettings['MESSAGE'];
$mail->isHTML(false);
} else if($wuMail_Config->ForceHTML === true && !in_array("HTML", $submittedOpts)){ # ForceHTML is true, and wuMail received no HTML template to use: ... use default:
$templateVarFind = array("{!messageInsert!}", "{!siteNameInsert!}");
$templateVars = array($mailSettings['MESSAGE'], $wuMail_Config->SiteName);
$mailSettings['HTML'] = str_replace($templateVarFind, $templateVars, $mailSettings['HTML']); // insert variables into default wuMail HTML template
$mail->isHTML(true);
} else {
$mail->isHTML(true);
}
$mail->Subject = $mailSettings['SUBJECT'];
if($mailSettings['HTML'] == $mailSettings['MESSAGE']){
$mail->Body = $mailSettings['MESSAGE'];
} else {
$mail->Body = $mailSettings['HTML'];
$mail->AltBody = $mailSettings['MESSAGE'];
}
$mail->send();
return true;
} catch (Exception $e) {
return 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}
} // wuMail end
?>
最后,在config.php和wuMail.php中设置了所有这些之后,我们就可以开始使用wuMail()函数了
require_once "wuMail/config.php";
$mailData = array(
"TOADDRESS" => "userToSendTo@email.com",
"TONAME" => "First Name",
"SUBJECT" => "Registration",
"MESSAGE" => "Hello this is a message",
"HTML" => "Hello this is a <strong>message</strong>",
"FROMADDRESS" => "from@website.com",
"FROMNAME" => "Admin Mail",
"REPLYADDRESS" => "donotreply@email.com"
);
if(wuMail($mailData) === true){
// mail sent
} else {
// mail not successful, change SMTPDebug to = 4 in config.php to bug test
}
如您所见,并非所有参数都必须提供...在$ mailData数组中,您可以提供:
$mailSettings = array(
"TOADDRESS" => $wuMail_Config->DefaultToAddress,
"TONAME" => $wuMail_Config->DefaultToName,
"CC" => $wuMail_Config->DefaultCC,
"BCC" => $wuMail_Config->DefaultBCC,
"SUBJECT" => $wuMail_Config->DefaultSubject,
"MESSAGE" => $wuMail_Config->DefaultMessage,
"HTML" => $wuMail_Config->DefaultHTML,
"FROMADDRESS" => $wuMail_Config->DefaultFromAddress,
"FROMNAME" => $wuMail_Config->DefaultFromName,
"REPLYADDRESS" => $wuMail_Config->DefaultReplyAddress,
"REPLYNAME" => $wuMail_Config->DefaultReplyName
);
如您所见,当您想发送邮件时,可以输入很多自定义参数到wuMail($ data)。如果你留下任何东西,它默认为config.php中针对该特定设置的设置!回复地址/姓名&amp;地址/姓名&amp;从名称/地址全部是动态的,因此如果您提供自定义地址(但没有名称),它将不会使自定义电子邮件与默认名称不匹配。它只是将该信息作为空白发送。
如果您需要,可以下载wuMail:
http://nerdi.org/stackoverflow/wuMail.zip
wuMail自带打包的PHPMailer并且100%免费...显然:)