簡體   English   中英

使用PHP從網頁發送電子郵件

[英]Sending email from webpage using PHP

當我嘗試向自己的個人電子郵件發送電子郵件時,在頁面上顯示

注意:第49行上的form-handler.php中的未定義變量:use_smtp錯誤。 再試一次

當我查看我的代碼時,我找不到錯誤的地方。

這是代碼:

<?php
include('SMTPClass.php');

$use_smtp=0; // $use_smtp=false;
$use_smtp=1; // $use_smtp=true;
$emailto = 'benfica.goed3@gmail.com';
// retrieve from parameters
$emailfrom = isset($_POST["email"]) ? $_POST["email"] : "";
$nocomment = isset($_POST["nocomment"]) ? $_POST["nocomment"] : "";
$subject = 'Email de EcoLover';
$message = '';
$response = '';
$response_fail = 'There was an error verifying your details.';

    // Honeypot captcha
    if($nocomment == '') {
        $params = $_POST;
        foreach ( $params as $key=>$value ){
            if(!($key == 'ip' || $key == 'emailsubject' || $key == 'url' || $key == 'emailto' || $key == 'nocomment' || $key == 'v_error' || $key == 'v_email')){
                $key = ucwords(str_replace("-", " ", $key));

                if ( gettype( $value ) == "array" ){
                    $message .= "$key: \n";
                    foreach ( $value as $two_dim_value )
                    $message .= "...$two_dim_value<br>";
                }else {
                    $message .= $value != '' ? "$key: $value\n" : '';
                }
            }
        }   
    $response = sendEmail($subject, $message, $emailto, $emailfrom);

    } else {
        $response = $response_fail;
    }
echo $response;
// Run server-side validation
function sendEmail($subject, $content, $emailto, $emailfrom) {
    global $use_smtp;
$from = $emailfrom;
$response_sent = 'Obrigado pela sua sugestão.';
$response_error = 'Erro. Tente novamente';
$subject =  filter($subject);
$url = "Origin Page: ".$_SERVER['HTTP_REFERER'];
$ip = "IP Address: ".$_SERVER["REMOTE_ADDR"];
$message = $content."\n$ip\r\n$url";
// Validate return email & inform admin
$emailto = filter($emailto);

// Setup final message
$body = wordwrap($message);
if($use_smtp){
    $SmtpServer = 'SMTP SERVER';
    $SmtpPort = 'SMTP PORT';
    $SmtpUser = 'SMTP USER';
    $SmtpPass = 'SMTP PASSWORD';
    $to = $emailto;
    $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
    $SMTPChat = $SMTPMail->SendMail();
    $response = $SMTPChat ? $response_sent : $response_error;

} else {
    // Create header
    $headers = "From: $from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=utf-8\r\n";
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
    // Send email
    $mail_sent = @mail($emailto, $subject, $body, $headers);
    $response = $mail_sent ? $response_sent : $response_error;

}
return $response;
}
    // Remove any un-safe values to prevent email injection
function filter($value) {
$pattern = array("/\n/", "/\r/", "/content-type:/i", "/to:/i", "/from:/i", "/cc:/i");
$value = preg_replace($pattern, "", $value);
return $value;
}

exit;
?>

現在該代碼可以正常工作了,但是另一頁的代碼卻給了我類似以下的錯誤:

警告:fsockopen()期望參數2長,在第29行的/opt/lampp/htdocs/EcoLover/SMTPClass.php中給出的字符串

注意:未定義的變量:在第61行的/opt/lampp/htdocs/EcoLover/SMTPClass.php中討論錯誤。 再試一次。

這是另一頁的代碼:

<?php

class SMTPClient
{

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{

$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;

$this->subject = $subject;
$this->body = $body;

if ($SmtpPort == "") 
{
$this->PortSMTP = 25;
    }else{
$this->PortSMTP = $SmtpPort;
}
    }

function SendMail ()
{

if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
{

       fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");  
       $talk["hello"] = fgets ( $SMTPIN, 1024 ); 

       fputs($SMTPIN, "auth login\r\n");
       $talk["res"]=fgets($SMTPIN,1024);
        fputs($SMTPIN, $this->SmtpUser."\r\n");
        $talk["user"]=fgets($SMTPIN,1024);

        fputs($SMTPIN, $this->SmtpPass."\r\n");
        $talk["pass"]=fgets($SMTPIN,256);

       fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");  
       $talk["From"] = fgets ( $SMTPIN, 1024 );  
       fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");  
       $talk["To"] = fgets ($SMTPIN, 1024); 

       fputs($SMTPIN, "DATA\r\n");
        $talk["data"]=fgets( $SMTPIN,1024 );

        fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
        $talk["send"]=fgets($SMTPIN,256);

       //CLOSE CONNECTION AND EXIT ... 

       fputs ($SMTPIN, "QUIT\r\n");  
       fclose($SMTPIN); 
     //  
}

return $talk;
}
}
?>

問題是您要設置

$use_smtp = '0';

在此功能之外,而該功能對此一無所知

function sendEmail($subject, $content, $emailto, $emailfrom) {

兩種選擇:使其全球化

function sendEmail($subject, $content, $emailto, $emailfrom) {
    global $use_smtp;
    // etc

或將其傳遞給函數

function sendEmail($subject, $content, $emailto, $emailfrom, $use_smtp) {
//                                                            ^ add this

像這樣

sendEmail($subject, $content, $emailto, $emailfrom, $use_smtp);
//                                                   ^ add this

請不要添加此行 error_reporting(E_ALL ^ E_NOTICE); 正如別人建議的那樣。 沒有通知,您將不會立即知道問題所在。 如果您不熟悉編程,那么必須給您盡可能多的信息。

在旁邊:

您可以使用布爾值或0和1, $use_smtp不必是字符串。

$use_smtp=0; // $use_smtp=false;
$use_smtp=1; // $use_smtp=true;

而且您無需檢查它是否等於“ 1”。
0'0'false均為假。

if ($use_smtp)

正如我從您的評論中看到的那樣,SMTP可以正常工作,並且此通知困擾着您。 您正在使用完整的錯誤日志記錄/調試/等環境下工作...在PHP文件的開頭添加error_reporting,如下所示:

   <?PHP
    error_reporting(E_ALL ^ E_NOTICE); 

編輯:

您應該在函數中創建全局$ use_smtp。 抱歉,我對此疏忽了。

function sendEmail($subject, $content, $emailto, $emailfrom) {
global $use_smtp;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM