dongou0524 2018-06-18 16:18
浏览 47
已采纳

在PHP和Localhost中发送电子邮件[重复]

This question already has an answer here:

i've searched for hours for a solution but i just don't get it. I'm fairly new to PHP, I'm doing a project where i need an email to be sent, the problem is that I can't figure how to do it. Others who had the same problem, fixed it by configuring the php.ini and the sendemail.ini

This is my current code for testing:

<?php

$subject="Hi There!!";
$to="mygmail@gmail.com";
$body="This is a demo email sent using PHP on XAMPP";
if (mail($to,$subject,$body)){
 echo "Mail sent successfully!";
}
else{
 echo "Mail not sent!";
}

?>

I receive the "Mail sent successfully" but there is nothing on my inbox, not even on the spam folder. I also tried to use PHPMailer but (like i said) im fairly new to php and i don't get how to enable it on my code. What do you guys think?

</div>
  • 写回答

1条回答 默认 最新

  • douba4275 2018-06-18 16:25
    关注
    require 'PHPMailer/PHPMailer.php';
    require 'PHPMailer/Exception.php';
    require 'PHPMailer/OAuth.php';
    require 'PHPMailer/POP3.php';
    require 'PHPMailer/SMTP.php';
    
    $mail = new PHPMailer;
    
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'user@example.com';
    $mail->Password = '******';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    
    $mail->setFrom('info@example.com', 'CodexWorld');
    $mail->addReplyTo('info@example.com', 'CodexWorld');
    
    // Add a recipient
    $mail->addAddress('john@gmail.com');
    
    // Add cc or bcc 
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
    
    // Email subject
    $mail->Subject = 'Send Email via SMTP using PHPMailer';
    
    // Set email format to HTML
    $mail->isHTML(true);
    
    // Email body content
    $mailContent = "<h1>Send HTML Email using SMTP in PHP</h1>
        <p>This is a test email has sent using SMTP mail server with PHPMailer.</p>";
    $mail->Body = $mailContent;
    
    // Send email
    if(!$mail->send()){
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }else{
        echo 'Message has been sent';
    }
    

    php mailer library https://github.com/PHPMailer/PHPMailer

    follow following link to understand php mailer https://www.codexworld.com/send-html-email-php-gmail-smtp-phpmailer/

    directory structure

    -root (your proj root directory from where your php file runs)
      -index.php (file with above code)
      -PHPMailer(folder contain mailer library)
         -php mailer files (go to git repo, download and copy all files from src folder)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?