使用PHP从网页发送电子邮件

时间:2014-03-16 16:55:02

标签: php email smtp

在我的页面上,当我尝试向我自己的个人电子邮件发送电子邮件时,它说

  

注意:未定义的变量:第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为long,第29行的/opt/lampp/htdocs/EcoLover/SMTPClass.php中给出了字符串

     

注意:未定义的变量:第61行的/opt/lampp/htdocs/EcoLover/SMTPClass.php中的talk错误。再试一次。

以下是其他页面的代码:

<?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;
}
}
?>

2 个答案:

答案 0 :(得分:2)

问题是您正在设置

$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;

而且你不需要检查它是否等于&#39; 1&#39;。 0'0'false都是假的。

if ($use_smtp)

答案 1 :(得分:-2)

当我从你的评论中看出SMTP正在运行时,这个通知会让你烦恼。您正在环境中使用完整的错误记录/调试/等...在PHP文件的开头添加error_reporting,如下所示:

   <?PHP
    error_reporting(E_ALL ^ E_NOTICE); 

编辑:

你应该在你的函数中创建全局$ use_smtp。对不起,我对此很过分了。

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