【问题标题】:Update PHP mail code to use SMTP instead of php mail() function更新 PHP 邮件代码以使用 SMTP 而不是 php mail() 函数
【发布时间】:2018-11-15 09:34:45
【问题描述】:

我有一个用于网站的 php 代码文件,用于使用 php 中的 mail() 函数从 Web 应用程序发送电子邮件。

我决定使用 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 代码。

非常感谢。

【问题讨论】:

  • 代码有问题吗?
  • 我想删除旧代码并使用新的stmp。我对php不是很熟悉,我认为旧代码中的变量应该在新部分中更新,因为它使用网络表单发送电子邮件。
  • 你得到这个工作的艾哈迈德了吗?我刚刚在我的网站上实现了 PHPMailer,并且正在替换 mail()。如果没有,我可以添加另一个答案

标签: php smtp phpmailer html-email


【解决方案1】:

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

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

  1. 那么你必须先将phpmailer库安装在你代码库的根目录(或任何地方,但在你的代码中包含/引用文件时,请确保路径正确)。

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

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

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

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

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

    $mail->端口 = 465;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2011-02-03
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多