如何在不提交任何表单的情况下以php发送电子邮件


How to Send email in php without any submission of form?

我正在用php编写脚本,其中我想在不提交任何表单的情况下将邮件发送到特定地址。

    <?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type:text/html;charset=UTF-8" . "'r'n";
// More headers
$headers .= 'From: <webmaster@example.com>' . "'r'n";
$headers .= 'Cc: myboss@example.com' . "'r'n";
mail($to,$subject,$message,$headers);
?>

所以请任何人帮助我如何在不提交任何表格的情况下将电子邮件发送到电子邮件地址,而无需在 PHP 中提交任何表格。

您不需要表单即可发送电子邮件。通过调用函数

mail($to,$subject,$message,$headers);

您会自动发送电子邮件。表单用于收集您要发送的信息。如果你已经有了这些信息,你所要做的就是在你的四个变量中填写信息:$to$subject$message$headers,然后使用该函数:

mail($to,$subject,$message,$headers);

让我知道这是否有帮助! :)

如果您不希望脚本在表单提交时执行,那么您还有另外两个选择。

将指向此页面的链接放在应用程序的另一个页面中。

<a href="path-to-your-mail-sending-file">Send Email</a>

或从另一个 PHP 脚本重定向到此页面:

header("Location: path-to-your-mail-sending-file");

此外,您还应该考虑使用PHPMailer,因为它比默认的mail函数更具可定制性和可靠性

您需要使用将在特定时间触发的 cronjob。如果您的主机不提供对 cronjobs 的访问权限,您可以使用:

https://www.setcronjob.com/

尝试使用 Ajax

$(document).load(function()
    {
        $.ajax({
            url: send_email.php,
            type:'POST',
            data:
            {
                email: email_address,
                message: message
            },
            success: function(msg)
            {
                alert('Email Sent');
            }               
        });
    });

将您的电子邮件代码放入send_email.php