[英]SilverStripe SMTP authentication instead of PHP mail() function
[英]Update PHP mail code to use SMTP instead of php mail() function
我有一個在網站中使用的 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 代碼。
非常感謝。
問題不是很清楚,但根據我在這里的理解是解決方案:)
如果您想用 PHPMAILER 替換默認的 mail() 函數:
然后,您必須首先將 phpmailer 庫安裝在代碼庫的根目錄(或任何地方,但在包含/引用代碼中的文件時,請確保路徑正確)。
安裝后,您將轉到調用 mail() 函數的代碼。 將 mail() 函數替換為您在上面共享的代碼示例。
然后替換代碼中的以下變量:
$mail->Host = 'smtp.domain.com'; // 指定主備SMTP服務器
$mail->用戶名 = '用戶名'; // SMTP用戶名
$mail->Password = '密碼'; // SMTP 密碼
$郵件->端口= 465;
如果您發現 SMTP 的挑戰,請使用基於 API 的電子郵件發送代碼庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.