Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用函数发送html邮件_Php_Html_Phpmailer - Fatal编程技术网

Php 使用函数发送html邮件

Php 使用函数发送html邮件,php,html,phpmailer,Php,Html,Phpmailer,我已经在这方面工作了很长时间,只是找不到问题所在。希望有人能帮助我 我已经为我的邮件脚本创建了一个函数。该函数位于有用的\u functions.inc.php <?php include ('mailer/class.phpmailer.php'); function sendmail($mail) { $mail = new PHPMailer(true); if (($_SERVER["REQUEST_METHOD"] == "POST") && $g

我已经在这方面工作了很长时间,只是找不到问题所在。希望有人能帮助我

我已经为我的邮件脚本创建了一个函数。该函数位于
有用的\u functions.inc.php

<?php
include ('mailer/class.phpmailer.php');

function sendmail($mail)
{
  $mail = new PHPMailer(true);  

  if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== "")
  {
    $full_name  = "Sofie Doe";
    $email      = "sofie@gmail.com";
    $subject    = "HTML test mail.";
    $text_message    = "Hello ".$full_name.",<br /><br /> This is a test email in html.";         

    $message  = "<html><body>";

    $message .= "<table width='100%' bgcolor='#e0e0e0' cellpadding='0' cellspacing='0' border='0'>";

    $message .= "<tr><td>";

    $message .= "<table align='center' width='100%' border='0' cellpadding='0' cellspacing='0' style='max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'>";

    $message .= "<thead>
          <tr height='80'>
            <th colspan='4' style='background-color:#f5f5f5; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#333; font-size:34px;' >TEST</th>
          </tr>
          </thead>";


    $message .= "</table>";

    $message .= "</td></tr>";
    $message .= "</table>";

    $message .= "</body></html>";

    try
    {
      $mail->IsSMTP(); 
      $mail->isHTML(true);
      $mail->SMTPDebug  = 0;                     
      $mail->SMTPAuth   = true;                  
      $mail->SMTPSecure = "STARTTLS";                 
      $mail->Host       = "mailout.one.com";      
      $mail->Port       = 587;             
      $mail->AddAddress($email);
      $mail->Username   ="joe@gmail.com";  
      $mail->Password   ="Password";            
      $mail->SetFrom("joe@gmail.com");
      $mail->AddReplyTo("joe@gmail.com");
      $mail->Subject    = $subject;
      $mail->Body     = $message;
      $mail->AltBody    = $message;
    }
  }
}

?>
当我运行
test.php
时,我不断收到HTTP错误500

我已经用一个简单的html表单测试了这个邮件程序,并用同样的方法调用它,它就工作了。 我的
test.php
在我删除
sendmail($mail)
并删除我的
usfuction.inc.php

<?php
include ('mailer/class.phpmailer.php');

function sendmail($mail)
{
  $mail = new PHPMailer(true);  

  if (($_SERVER["REQUEST_METHOD"] == "POST") && $geslachtErr== "" && $voornaamErr== "" && $familienaamErr== "" && $emailErr== "" && $telErr== "" && $afileErr== "")
  {
    $full_name  = "Sofie Doe";
    $email      = "sofie@gmail.com";
    $subject    = "HTML test mail.";
    $text_message    = "Hello ".$full_name.",<br /><br /> This is a test email in html.";         

    $message  = "<html><body>";

    $message .= "<table width='100%' bgcolor='#e0e0e0' cellpadding='0' cellspacing='0' border='0'>";

    $message .= "<tr><td>";

    $message .= "<table align='center' width='100%' border='0' cellpadding='0' cellspacing='0' style='max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'>";

    $message .= "<thead>
          <tr height='80'>
            <th colspan='4' style='background-color:#f5f5f5; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#333; font-size:34px;' >TEST</th>
          </tr>
          </thead>";


    $message .= "</table>";

    $message .= "</td></tr>";
    $message .= "</table>";

    $message .= "</body></html>";

    try
    {
      $mail->IsSMTP(); 
      $mail->isHTML(true);
      $mail->SMTPDebug  = 0;                     
      $mail->SMTPAuth   = true;                  
      $mail->SMTPSecure = "STARTTLS";                 
      $mail->Host       = "mailout.one.com";      
      $mail->Port       = 587;             
      $mail->AddAddress($email);
      $mail->Username   ="joe@gmail.com";  
      $mail->Password   ="Password";            
      $mail->SetFrom("joe@gmail.com");
      $mail->AddReplyTo("joe@gmail.com");
      $mail->Subject    = $subject;
      $mail->Body     = $message;
      $mail->AltBody    = $message;
    }
  }
}

?>

为什么这不起作用?

为什么不通过修改
尝试{}
以包含
$mail->send(),让电子邮件“自动发送”

您的“尝试”将变成:

try
{
  $mail->IsSMTP(); 
  $mail->isHTML(true);
  $mail->SMTPDebug  = 0;                     
  $mail->SMTPAuth   = true;                  
  $mail->SMTPSecure = "STARTTLS";                 
  $mail->Host       = "mailout.one.com";      
  $mail->Port       = 587;             
  $mail->AddAddress($email);
  $mail->Username   ="joe@gmail.com";  
  $mail->Password   ="Password";            
  $mail->SetFrom("joe@gmail.com");
  $mail->AddReplyTo("joe@gmail.com");
  $mail->Subject    = $subject;
  $mail->Body     = $message;
  $mail->AltBody    = $message;
  $mail->Send();
  echo "Message Sent OK\n";
  } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
  } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
  }

500是服务器错误;检查日志和/或启用错误报告您的
try
语句缺少
catch()
块。这是一个语法错误。一个好的IDE会警告你这一点。@Fred ii-启用错误报告很少有助于处理500个错误。如果出现语法错误,将不会执行
error\u reporting()
调用。@如果您有
try{…}
,则它后面至少需要有一个
catch
块。
try
的全部意义在于,您可以编写在出错时运行的代码。@WouterS没有经过投票的答案,因此我不明白您为什么不能删除另一个问题。如果它仍然不允许您,您可以将您的(其他)问题标记为版主,以便为您删除它,如果您希望删除它。