如何在没有提交表格的情况下在php发送电子邮件?

时间:2016-04-22 10:50:28

标签: php email phpmailer

我正在使用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中提交任何表格。

4 个答案:

答案 0 :(得分:1)

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

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

您自动发送电子邮件。表单用于收集您要发送的信息。如果您已经拥有这些信息,那么您只需填写以下四个变量中的信息:$to$subject$message$headers,然后使用功能:

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

如果有帮助,请告诉我! :)

答案 1 :(得分:0)

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

在您的应用程序的另一个页面中放置此页面的链接。

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

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

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

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

答案 2 :(得分:0)

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

https://www.setcronjob.com/

答案 3 :(得分:0)

尝试使用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