如何从localhost发送电子邮件到我的Gmail

时间:2013-12-15 18:48:50

标签: php

我正在使用wamp,我想从youtube上的教程测试一个电子邮件功能,但它不会发送,我得到的所有代码都正确,它发送了你的电子邮件已发送的消息。我甚至改变了php.ini文件 SMTP = smtp.gmail.com sendmail_from = philipnagel511@gmail.com 任何建议

如果你可以把我写成电子邮件功能,这是我的 这是一个忘记密码我正在尝试向用户发送新密码

<?php
    if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
?>
    <p>Thanks, we've emailed you.</p>
<?php
} else {
$mode_allowed = array('email', 'password');
if (isset($_GET['mode']) === true && in_array($_GET['mode'], $mode_allowed) === true) {
    if (isset($_POST['email']) === true && empty($_POST['email']) === false) {
        if (user_exists($_POST['email']) === true) {
            recover($_GET['mode'], $_POST['email']);
            header('Location: recover.php?success');
            exit();
        } else {
            echo '<p>Ooops, we couldn\'t find that email address</p>';
        }
    }
?>

function recover($mode, $email) {
    $mode    = sanitize($mode);
    $email   = sanitize($email);

    $user_data = user_data(user_id_from_email($email),'user_id', 'first_name', 'username');

    if ($mode == 'username') {
        email($email, 'Your username', "Hello " . $user_data['first_name'] . ",\n\nYour username is: ". $user_data['username'] . "\n\n-phpacademy");
    } else if ($mode == 'password') {
        $generated_password = substr(md5(rand(999, 999999)), 0, 8);
        change_password($user_data['user_id'], $generated_password);
        email($email, 'Your password recovery', "Hello " . $user_data['first_name'] . ",\n\nYour pasword is: ". $generated_password . "\n\n-phpacademy");
    }
}

function email($to, $subject, $body, $headers) {
    mail($to, $subject, $body, $headers);
}

2 个答案:

答案 0 :(得分:0)

关注this guide它会帮助您度过难关。 除了使用projectpier之外,确保你做了所有事情,你不需要它。你将使用Php发送邮件。

示例Php代码:

$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
    if (mail($mailto, $subject, $message, $header)) {
        echo "Mail successfully sent with given data"; // or use booleans here
    } else {
        echo "Mail sending failed! Please make sure all data is correct and you are connected to the internet";
    }

确保正确设置所有变量

答案 1 :(得分:0)

就发送电子邮件而言,我建议使用PHPMAILER。你可以在phpmailer

获得相同的内容
function send_email($recipient, $sender_email,$sender_name, $subject, $message){ 
require_once("phpmailer/class.phpmailer.php");
$mail   = new PHPMailer();
$body   = $message;
$mail->Host         =  'smtp.googlemail.com';
$mail->SMTPAuth     =  true;
$mail->Username     =  'your gmail id';
$mail->Password     =  'your gmail password';
$mail->IsSMTP();
$mail->FromName     = $sender_name;
$mail->Port         = 465;
$mail->SMTPSecure   = 'ssl';
$mail->From         = $sender_email;
$mail->Subject      = $subject;
$mail->AltBody      = strip_tags($message);
$mail->MsgHTML($body);
$mail->AddAddress($recipient);


if ( ! $mail->Send())
{
    return false;
}
else
{
    return true;
}

}

我想这会解决你的问题.. 另外,我建议您先按照方法确定问题。首先检查您的邮件功能是否正常,只需传递硬编码值即可。一旦您确认相同,然后通过您的表单/代码传递值,并再次检查问题是什么。