php邮件程序无法正常工作

时间:2014-09-09 09:28:21

标签: php

这是我的phpmailer代码,它不起作用,不知道我在哪里得到错误。 我必须在某些脚本中嵌入此代码,我认为对于gmail端口号port no 465

<?php 
if(isset($_POST['submit'])) {
require_once('phpmailer/class.phpmailer.php');

$email=$_POST['email'];
$subject1=$_POST['subject'];
$message=$_POST['message'];
smtpmailer($email,$subject1,$message);

function smtpmailer($to,$subject,$body) { 

    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";  
    $mail->Port = 465; 
    $mail->Username = "aman****589@gmail.com";  
    $mail->Password = "*****02589";  

    $mail->From     = "aman***589@gmail.com";
    $mail->FromName = "Cor****tions";

    $mail->Subject = $subject;
    $mail->Body = $body;

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
}
}
}
?><html><body>
<form method="post" action="index.php">
Email: <input name="email" id="email" type="text" /><br />
Subject:<br />
<textarea name="subject" id="subject" rows="2" cols="40"></textarea><br />
Message:<br/>
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>

任何人都可以帮忙吗?我在哪里得到错误? 当我运行这个脚本时给出错误 -

Fatal error: Call to undefined function smtpmailer() in C:\xampp\htdocs\phpm\index.php on line 8

2 个答案:

答案 0 :(得分:2)

在调用函数之前,必须使用其参数声明一个函数。目前,您的程序正在搜索函数smtpmailer() - 但它尚未定义。

<?php 
if(isset($_POST['submit'])) {
require_once('phpmailer/class.phpmailer.php');

$email=$_POST['email'];
$subject1=$_POST['subject'];
$message=$_POST['message'];

function smtpmailer($to,$subject,$body) { 

    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";  
    $mail->Port = 465; 
    $mail->Username = "aman****589@gmail.com";  
    $mail->Password = "*****02589";  

    $mail->From     = "aman***589@gmail.com";
    $mail->FromName = "Cor****tions";

    $mail->Subject = $subject;
    $mail->Body = $body;

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
}
}
smtpmailer($email,$subject1,$message);
}

?><html><body>
<form method="post" action="index.php">
Email: <input name="email" id="email" type="text" /><br />
Subject:<br />
<textarea name="subject" id="subject" rows="2" cols="40"></textarea><br />
Message:<br/>
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>

答案 1 :(得分:0)

在调用之前声明函数。