【发布时间】:2015-08-23 12:45:34
【问题描述】:
我正在尝试使用 phpmailer 添加 smtp 身份验证。我是 php 新手,我正在努力实现这一目标。 我有这个代码:
<?php
if( isset($_POST['name']) )
{
$to = $_POST['budget'].',test@test.ro';
$subject = 'Programare servicii';
$message = 'Nume: ' . $_POST['name'] . "\n" .
'Prenume: ' . $_POST['company']. "\n" .
'E-mail: ' . $_POST['email']. "\n" .
'Telefon: ' . $_POST['phone']. "\n\n" .
'Doresc: ' . $_POST['interested']. "\n" .
'Magazin: ' . $_POST['budget']. "\n" .
'Incepand cu: ' . $_POST['start']. "\n" .
'Pana la: ' . $_POST['finish']. "\n" .
'Observatii: ' . $_POST['comment']. "\n\n\n";
$num = md5(time());
$headers = 'From:' . $_POST['name'] . "\r\n";
$headers .= 'Reply-To:' . $_POST['email'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=$num\r\n";
$headers .= "--$num\r\n";$headers .= "Content-Type: text/plain\r\n";
$headers .= "Content-Transfer-Encoding:8bit\r\n\n";
$headers .= "$message\r\n";
$headers .= "--$num\r\n";
if( isset($_FILES['file']['tmp_name']) )
{
$file = fopen($_FILES['file']['tmp_name'], 'r');
$size = $_FILES['file']['size'];
$content = fread($file, $size);
$encoded_content = chunk_split(base64_encode($content));
$headers .= "Content-Type: ". $_FILES['file']['type'] ."; ";
$headers .= 'name="' . $_FILES['file']['name'] . '"' . "\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= 'filename="' . $_FILES['file']['name'] . '"' . "\r\n\n";
$headers .= "$encoded_content\r\n";
$headers .= "--$num--";
}
mail($to, $subject, '', $headers);
}
?>
id如何添加认证?有任何想法吗?我阅读了 phpmailer 示例,并且能够使用 phpmailer 发送经过身份验证的电子邮件。问题是我无法让上面的代码与 smtp 身份验证一起使用。我的表单中有一个下拉菜单,用户可以从那里选择一个商店,脚本将向该商店发送电子邮件。当我将 $_POST['email'] 添加到 $mail->addAddress 时,没有任何反应。 感谢您的宝贵时间,
波格丹
更新 1: 我设法做到这一点:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = true;
$mail->Username = 'test@test.ro';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->addAddress('need to get data from form', 'iohan test');
$mail->addReplyTo('test@test.ro', 'Information');
$mail->addCC('test@test.ro');
$mail->isHTML(true);
$mail->Subject = 'test iohan';
$mail->Body = 'bla bla bla';
$mail->AltBody = 'bla bla';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
问题是如何让它发挥作用。我有一个包含多封电子邮件的下拉菜单,我需要将电子邮件发送到用户选择的电子邮件。
【问题讨论】:
-
请将您的代码减少到解释您的问题所需的最低限度,并添加更多关于您遇到的问题的详细信息。
-
我正在尝试使用 php mailer 将 smtp 身份验证添加到上面的 php 代码中。