php - 如何使用邮件功能使用 php 表单脚本将邮件从我的网站电子邮件发送到 gmail?

标签 php linux function email gmail

我在下面有一个简单的联系表---

    <html>

   <head>
      <title>Sending HTML email using PHP</title>
   </head>

   <body>

      <?php
         $to = "contact@mydomain.com";
         $subject = "This is subject";

         $message = "<b>This is HTML message.</b>";
         $message .= "<h1>This is headline.</h1>";

         $header = "From:jayaryaa@gmail.com \r\n";
         //$header .= "Cc:no-reply@skynetinfosolution.com \r\n";
         $header .= "MIME-Version: 1.0\r\n";
         $header .= "Content-type: text/html\r\n";

         $retval = mail ($to,$subject,$message,$header);

         if( $retval == true ) {
            echo "Message sent successfully...";
         }else {
            echo "Message could not be sent...";
         }
      ?>

   </body>
</html>

当我将我的网站电子邮件放在 $to 的位置时,这个脚本工作正常,当我在 FROM 的位置输入我的 gmail id 时,我的问题是如何使用邮件功能从我的网站电子邮件发送电子邮件到我的 gmail ? ,我尝试用我的 gmail id 代替 $to,用我的网站电子邮件代替 From 但它不起作用,我的网站是一个共享的 linux 主机。

最佳答案

看看 PHPMailer Github .当您使用 PHP 邮件功能时,您是直接从您的 Web 服务器发送电子邮件。建议通过 SMTP 发送邮件,因为电子邮件是从邮件服务器而不是本地服务器发送的。此脚本可让您通过 Google 的 Gmail 服务器发送邮件。下面的例子

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 * This uses traditional id & password authentication - look at the gmail_xoauth.phps
 * example to see how to use XOAUTH2.
 * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
 */
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
    //Section 2: IMAP
    //Uncomment these to save your message in the 'Sent Mail' folder.
    #if (save_mail($mail)) {
    #    echo "Message saved!";
    #}
}
//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
    //You can change 'Sent Mail' to any other folder or tag
    $path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
    //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
    $imapStream = imap_open($path, $mail->Username, $mail->Password);
    $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
    imap_close($imapStream);
    return $result;
}

关于php - 如何使用邮件功能使用 php 表单脚本将邮件从我的网站电子邮件发送到 gmail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51096918/

相关文章:

sql-server - 在函数内执行WITH语句

php - 如何在mysql中获得最高分的SUM并按类别排序?

php - 从 MySQL 返回结果时的数字顺序

c - GCC 因 fatal error 终止操作

c - 是否可以在 C 中编写与类型无关的函数,并在实现中派生类型?

javascript - 递归函数 指数函数

php - 如何从 html 按钮启动条码扫描器

php - 将 PHP 与 MySQL 数据库一起使用,如果我不知道列是什么,如何从一行中选择所有内容?

linux - 如何通过特定的网络接口(interface)发送?

linux - 当涉及到失败状态时,shell 函数可以充当命令吗?