使用相同的脚本在订阅时从html发送带有PHP的电子邮件

时间:2015-08-11 18:27:58

标签: php

当用户在我的HTML页面中订阅时,我想用PHP发送电子邮件确认。我有一个重定向页面,但我需要发送一封电子邮件,让他知道他确实订阅了。这是我的PHP。我可以用同样的PHP来做吗?

<?php
/* Set e-mail recipient */
$myemail  = "example@domain";
$subject  = "You have a new subscriber!";

/* Check all form inputs using check_input function */
$email    = check_input($_POST['email']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

An email has been submitted by:

E-mail: $email

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the success page */
header('Location: success.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

这是我正在使用的PHP!任何人都可以帮助!!!! 先谢谢

1 个答案:

答案 0 :(得分:1)

发送邮件的代码段

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

$ message可以是html标记

如果您正在寻找第三方

https://github.com/Synchro/PHPMailer