如何将PHP PEAR SMTP auth添加到电子邮件中

时间:2013-12-18 21:10:13

标签: php html smtp pear

我一直在这工作几个小时,无法弄明白。我试图添加smtp身份验证发送此电子邮件,它似乎不起作用。这是没有任何pear php身份验证的代码。

我已经在我的网络服务器上安装了梨邮件,但它仍然无效。

<?php
 if (isset($_POST['sendemail']))

{
// Start email send notification
$date = $_POST['date'];
$propertydescription = $_POST['propertydescription'];
$transactiontype = $_POST['transactiontype'];
$received = $_POST['recieved'];
$paid = $_POST['paid'];
$tj = $_POST['tj'];
$agentclientr = $_POST['agentclientr'];
$apdoc =$_POST['apdoc'];

                    $to = "$b5,xxxx@xxxx.net";  
                    $subject = "Automated email from J. xxxxProperties";

                    $message = "
                    <html>
<p>This email serves as confirmation that a new item has recently posted to your account.</p>
<p><a href='http://xxxxxxjlindsey.net/manage</a><br>
  Login: $b3</p>

<table border='0' cellpadding='4' cellspacing='+1' bgcolor='#CCCCCC'>
        <tbody>
          <tr bgcolor='#666666'>
            <td><strong><font size='1' face='Arial, Helvetica, sans-serif' color='#FFFFFF'>Date</font></strong></td>
            <td><strong><font size='1' face='Arial, Helvetica, sans-serif' color='#FFFFFF'>Property/Description</font></strong></td>
            <td><strong><font size='1' face='Arial, Helvetica, sans-serif' color='#FFFFFF'>TransactionType</font></strong></td>
            <td><strong><font size='1' face='Arial, Helvetica, sans-serif' color='#FFFFFF'>Applicable Document</font></strong></td>
            <td><strong><font size='1' face='Arial, Helvetica, sans-serif' color='#FFFFFF'>Received</font></strong></td>
            <td align='right'><strong><font size='1' face='Arial, Helvetica, sans-serif' color='#FFFFFF'>Paid</font></strong></td>
          </tr>
          <tr bgcolor='#FFFFFF'>
            <td><font size='1' face='Arial, Helvetica, sans-serif' color='#666666'>$date</font></td>
            <td><font size='1' face='Arial, Helvetica, sans-serif' color='#666666'>$propertydescription</font></td>
            <td><font size='1' face='Arial, Helvetica, sans-serif' color='#666666'>$transactiontype</font></td>
            <td><font size='1' face='Arial, Helvetica, sans-serif' color='#666666'>$apdoc</font></td>
            <td align='right'><font size='1' face='Arial, Helvetica, sans-serif' color='#666666'>$received</font></td>
            <td align='right'><font size='1' face='Arial, Helvetica, sans-serif' color='#666666'>$paid</font></td>
          </tr>
        </tbody>
      </table>
                    </html>
                    <p><font size='-2' face='Arial, Helvetica, sans-serif' color='red'>If this is the first time you've received an automated transmission from us, please send us an <a href='mailto:re@xxxxxx.net?Subject=Received email.'>email</a>.</font>";

                    // Always set content-type when sending HTML email
                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

                    // More headers
                    $headers .= 'From: <noreply@jlindsey.net>' . "\r\n";


                    mail($to,$subject,$message,$headers);
echo "<b><Center>Succesfully sent email to</b> $b5</center>";



                     // End email send notification
}
?>

2 个答案:

答案 0 :(得分:0)

尝试phpmailer它会解决您的问题。如果你在linux机器上,那么为邮件服务器安装postfix。

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';

答案 1 :(得分:0)

看到问题是“如何将PHP PEAR SMTP auth添加到电子邮件中”,我将解决这个问题 而不是建议你使用别的东西:)

因为您需要将HTML作为mime编码电子邮件发送,所以您还需要使用PEAR提供的Mail / mime包,所以我也包含了它。你可能不需要 设置$ port值 - 取决于您的邮件程序设置或是否可能在幕后使用gmail。

<?php
require_once "Mail.php";
require_once "Mail/mime.php";

$from = "<noreply@jlindsey.com>";
$to = "fred@example.com"; // the email address
$host = "smtp.gmail.com";
$port = "587";
// use the first example username if sending from gmail, as full email address 
// is required
$username = "fred.flintstone@gmail.com";
$username = "fred";
$password = "secure";
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$mailbody = "<html><body>...</body></html>";

$mime = new Mail_mime();
$mime->setHTMLBody($mailbody);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
    'smtp',array (
        'host' => $host,
        'auth' => true,
        'username' => $username,
        'password' => $password,
        'port' => $port
    )
);

// send email
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
    echo($mail->getMessage());
} else {
    echo "<b><Center>Succesfully sent email to</b>$to</center>";
}