【问题标题】:Sending email in PHP - Form用 PHP 发送电子邮件 - 表单
【发布时间】:2018-05-30 13:43:13
【问题描述】:

我一直在尝试使用 php 进行测试以通过表单发送电子邮件,但它对我不起作用。返回以下消息:发送电子邮件时出错。即变量 boolean $ sent 的值为 false。

我使用 Apache、XAMPP for Windows 10 和 Netbeans 8.2 开发环境

让我们看看是否有人可以帮助我,在此先感谢。

这是给出错误的代码:

<?php
$for = 'email@example.com';
$title = 'Sending email from PHP';
$message = '<html><head><title> Email with HTML </title></head>
            <body><h1> Email with HTML </h1>
            This is an email that is sent in HTML format
            <hr>Sent by my program in PHP</body></html>';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers. = 'Content-type: text / html; charset = utf-8 '. "\r\n";
$headers. = 'From: Name1 Name 2 <info@example.com>';
$sent = mail ($for, $title, $message, $headers);

if ($sent)
    echo 'Email sent correctly';
else
    echo 'Error in sending the email';
?>

【问题讨论】:

  • 这看起来确实是一个硬语法错误:$ for 等等不是有效的 PHP 代码
  • 您在...HTML format.' 之后没有正确连接,缺少.。 $headers 之后缺少分号。话虽如此,给自己找一个像样的 IDE,它会提前掌握这些东西。并启用 php 错误报告(如果您还没有)。
  • 你试过不带标题发送吗?
  • 复制粘贴代码的时候已经加了空格,没有加连接点。我还是没解决。
  • 如果我检查发送不带标题的电子邮件。

标签: php forms xampp phpmailer sendmail


【解决方案1】:

据我所知,您有一些连接问题,我不确定这些问题是否实际存在于您的代码中,或者是由于复制粘贴所致。尝试跟随,注意$header 连接和语句末尾的分号;。

<?php
$for = 'email@example.com';
$title = 'Sending email from PHP';
$message = '<html>'.
        '<head> <title> Email with HTML </title> </head>'.
        '<body> <h1> Email with HTML </h1>'.
        'This is an email that is sent in HTML format.'
    '<hr>'.
    'Sent by my program in PHP'.
    '</body>'.
    '</html>';
$headers = 'MIME-Version: 1.0'. "\ r \ n";
$headers .= 'Content-type: text / html; charset = utf-8 '. "\ r \ n";
$headers .= 'From: Name1 Name 2 <info@example.com>';
$sent = mail ($for, $title, $message, $headers);

if ($sent)
    echo 'Email sent correctly';
else
    echo 'Error in sending the email';
?>

【讨论】:

  • 这是传递给 mail() 函数的变量的值: email@example.com 从 PHP 发送电子邮件 带有 HTML 的电子邮件 这是一封以 HTM 格式发送的电子邮件,由我的PHP程序MIME版本:1.0 内容类型:text/html; charset = utf-8 From: Name1 Name 2 发送电子邮件时出错
  • 您是否在本地机器上配置了邮件设置?
  • 不,你能解释一下我必须做什么吗?
  • 那么,你需要从php.ini配置邮件设置,参考这个链接roshanbh.com.np/2007/12/…,或者你的框架中可能有一些smtp设置选项
  • 我想向在我的应用程序中注册的用户发送一封电子邮件。仅此而已。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
相关资源
最近更新 更多