来自php脚本的调用函数不起作用

时间:2015-07-24 22:52:09

标签: php

我想在数据库中插入后发送电子邮件,所以我在一个单独的文件sendemail.php中定义了一个函数sendm:

<?php
function sendm($recipient, $subject, $messagetext)
{
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'myemail@gmail.com';
$mail->Password = 'mypassword';
$mail->SetFrom('myemail@gmail.com', 'me');
$mail->AddAddress($recipient);
$mail->Subject = $subject;

$message = '<html><body>';
$message .= '';
$message .= $messagetext;
$message .= '<table rules="all" width="600px">';
$message .= '<br>';
$message .= '<b>please dont reply.</b></td></tr>';
$message .= "</table>";
$message .= "</body></html>";

// add message body
$mail->MsgHTML($message);

try {
    $mail->Send();
    echo "Mail send successfully";
} catch (phpmailerException $e) {
    echo $e->getMessage();
} catch (Exception $e) {
    echo $e->getMessage();
}
}

我测试了它并且它可以工作,但是当从插入脚本(insertion.php)调用时,数据被插入数据库但没有发送电子邮件:

<?php
  include 'sendemail.php'; 
  header ('Content-type: text/html; charset=utf-8');
  //connection au serveur
  $cnx = mysql_connect( "localhost", "root", "passwd" ) ;

  //sélection de la base de données:
  $db  = mysql_select_db( "mydb" ) ;

  //récupération des valeurs des champs:

  $name     = $_POST["name"] ;
  //email:
  $email = $_POST["email"] ;

  mysql_set_charset("utf8");

  $sql = "INSERT  INTO demande (name, email)
            VALUES ('$name', '$email') " ;

  $requete = mysql_query($sql, $cnx) or die( mysql_error() ) ;

  if($requete)
  {
    echo("insert ok") ;
    $to = 'recipient@gmail.com';    
    $subject = 'test';
    $message = 'test message.<br>';
    sendm($to, $subject, $message);
  }
  else
  {
    echo("insert KO") ;
  }
?> 

谢谢

0 个答案:

没有答案