如何从本地主机向我的gmail发送电子邮件


How do i send an email from localhost to my gmail

im使用wamp,我想在youtube上测试一个教程中的电子邮件功能,但它不会发送,我得到了所有正确的代码,它发送了你的电子邮件已经发送的消息。我甚至更改了php.ini文件SMTP=SMTP.gmail.comsendmail_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);
}

遵循本指南,它将帮助您度过难关。确保你在那里做的一切,除了使用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";
    }

确保正确设置了所有变量

就电子邮件的发送而言,我建议使用PHPILER。你可以在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;
}

}

我想这会解决你的问题。。此外,我建议您先采用这种方法来识别问题。首先,通过传递硬编码的值来检查邮件功能是否正常工作。一旦您确认了相同的内容,然后通过表单/代码传递值,并再次检查问题所在。