我看到大多数PHPMailer问题都使用了IsSMTP(),但是我的工作是昨天在SO上发现的一个非常基本的例子(虽然似乎找不到历史中的链接)但是没有。
require_once("class.phpmailer.php");
$email = new PHPMailer();
if (isset($_FILES['upload']['size']))
{
echo 'file size: '.(basename($_FILES['upload']['size'])* 1024).'<br />';
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded<br />";
$email->AddAttachment($target_path);
}
else
{
echo "There was an error uploading the file, please try again!<br /> ".basename($_FILES['upload']['error']);
}
}
else
{
echo "No file was found for the upload.<br />";
}
$email->To = "me@this.com";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "Query from website";
$email->Body = $email_message;
//echo "mail built...<br />";
if (!$email->Send())
{
echo "Mailer error: " . $email->ErrorInfo;
}
我尝试过两次发送。代码执行正常,最后if
语句返回true。
我必须使用IsSMTP()吗?如果是这样,它是否必须通过SSL路由,就像我看到建议的here?
提前致谢?
编辑
我已添加SMTP所需的详细信息,如下所示:
$email->IsSMTP();
$email->Host = "localhost";
$email->Port = 465;
$email->SMTPAuth = true;
$email->Username = "bacon";
$email->Password = "4ndCh33se";
这似乎没有触发它发送。还没有报告任何错误。
修改
原来有一个错误,我在一个奇怪的地方有一个重定向,阻止我看到它。错误如下:
您必须至少提供一个不支持的邮件。
答案 0 :(得分:1)
最初,添加IsSMTP()似乎至关重要,并按其他答案实施。
然而,所有答案都未能解决一个难题中的一个问题。
PHPMailer要求至少添加1个地址,如下所示。我在这里使用的地址与To
属性中设置的地址相同:
$email->To = "me@this.com";
$email->AddAddress("me@this.com");
一旦完成,所有工作都完美无缺。
答案 1 :(得分:0)
如果您想通过smtp
发送邮件,则需要设置SMTP
用户名和密码以及其他设置。
此外,您还需要添加IsSMTP()
。
Gmail SMTP设置示例
$this->SwiftMailer->smtpType = 'tls';
$this->SwiftMailer->smtpHost = 'smtp.gmail.com';
$this->SwiftMailer->smtpPort = 587;
$this->SwiftMailer->smtpUsername = 'emailaddress@gmail.com';
$this->SwiftMailer->smtpPassword = 'gmailPassword';
$this->SwiftMailer->sendAs = 'html';
$this->SwiftMailer->from = 'sender@gmail.com';
$this->SwiftMailer->fromName = 'Sender Name';
$this->SwiftMailer->to = "receiver@anything.com";
$this->Email->sendAs = 'html';
答案 2 :(得分:0)
$email->To = "me@this.com";
$email->From = $email_from;
$email->FromName = $first_name;
$email->Subject = "Query from website";
$email->Body = $email_message;
除了你之外我还有以下内容。也许有帮助:
$email->IsSMTP(); // set mailer to use SMTP
$email->Host = "mail.webaddress.com"; // specify main and backup server
$email->SMTPAuth = true; // turn on SMTP authentication
$email->Username = "********"; // SMTP username
$email->Password = "********"; // SMTP password
我在class.phpmailer.php中没有改变任何内容
$email->IsSMTP(); // set mailer to use SMTP
$email->Host = "mail.webaddress.com"; // specify main and backup server
$email->SMTPAuth = false; // turn on SMTP authentication
$email->Port = 25
$email->SMTPSecure = "";
$email->Username = "********"; // SMTP username
$email->Password = "********"; // SMTP password