更新PHP邮件代码以使用SMTP而不是php mail()函数

时间:2018-11-15 09:34:45

标签: php smtp phpmailer html-email

我在网站上使用了一个php代码文件,以使用php中的mail()函数来处理从网络应用发送电子邮件。

我决定使用SMTP,而不是希望您检查旧代码的变量是否在新的SMTP上使用了

这是代码

<?php

// please only use the fields thata re present in the html form itself for now we have listed all possible ones

//// NEW CODE ////
require '../mail/PHPMailerAutoload.php';


$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.domain.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'username';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'SSL';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

$mail->setFrom('hello@example.com', 'Name');
$mail->addAddress('maher@domain.com', 'Location');     // Add a recipient                              // Name is optional
$mail->addReplyTo('hello@example.com', 'Name');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//$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;
} else {
echo 'Message has been sent';
}

//// OLD CODE ////

$to = "maher@domain.com";
if (isset($_POST)){

    $subject = "location system email";

    if ($_POST['fullname'] ! =''){
        $message = "Fullname: " . $_POST['fullname'];
    } else {
        $message = "First name: " . $_POST['fname'];
        $message = "Last name: " . $_POST['lname'];
    }
    $message .= "<br>Phone: " . $_POST['Phone'];
    $message .= "<br>Website: " . $_POST['website'];
    $message .= "<br>Email: " . $_POST['email'];
    $message .= "<br>Message: " . $_POST['message'];

};

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: " . $_POST['fullname'] . " <" . $_POST['email'] . ">". "\r\n";

if(mail($to, $subject, $message, $headers) ) {
    echo "ok";
} else {
    echo "error";
}

我想使用旧的代码变量并将其应用于新的SMTP代码。

非常感谢。

1 个答案:

答案 0 :(得分:-1)

问题不是很清楚,但是据我了解,这里的解决方法是:)

如果要用PHPMAILER替换默认的mail()函数:

  1. 然后,您必须首先将phpmailer库安装在代码库的根目录中(或其他任何位置,但在包括/引用代码中的文件时,请确保路径正确)。

  2. 安装完成后,您将转到调用mail()函数的代码。使用上面共享的代码示例替换mail()函数。

  3. 然后在代码中替换以下变量:

    $ mail-> Host ='smtp.domain.com'; //指定主要和备用SMTP服务器

    $ mail-> Username ='username'; // SMTP用户名

    $ mail-> Password ='password'; // SMTP密码

    $ mail-> Port = 465;

如果您发现SMTP挑战,请使用基于API的电子邮件发送代码库。