简体   繁体   English

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

[英]How do I send mail to gmail from my website email using php form script using mail function?

I have a simple contact form below--- 我在下面有一个简单的联系表-

    <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>

This script is working fine when I put my website email on place of $to , and when I enter my gmail id on place of FROM , my question is how can I send email to my gmail from my website email using mail function ? 当我将我的网站电子邮件放在$ to位置时,并且当我在FROM中输入我的gmail ID时,此脚本工作正常,我的问题是如何使用邮件功能从我的网站电子邮件向gmail发送电子邮件? ,I have tried by putting my gmail id in place of $to and my website email in place of From but it's not working , My website is a shared linux hosting . ,我尝试通过将gmail ID替换为$ to以及将我的网站电子邮件替换为From进行尝试,但无法正常工作,我的网站是共享的Linux托管服务器。

Take a look at the PHPMailer Github . 看一看PHPMailer Github When you use the PHP mail function, you are sending email directly from your web server.Sending mail via SMTP is recommended as email is sent from the mail server rather than local server. 使用PHP邮件功能时,您是直接从Web服务器发送电子邮件。建议通过SMTP发送邮件,因为电子邮件是从邮件服务器而不是本地服务器发送的。 This script lets you send messages via your Google's Gmail server. 使用此脚本,您可以通过Google的Gmail服务器发送邮件。 Example below 下面的例子

<?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;
}

Isn't because your host prevent emails sending from another domain for spam issues ? 是不是因为您的主机阻止垃圾邮件问题从另一个域发送电子邮件?

Eventually you can add a transfert rule from your Skynet to Gmail adress. 最终,您可以将传输规则从天网添加到Gmail地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用gmail从PHP发送邮件? - How to send a mail from PHP using gmail? 使用php脚本发送邮件(我希望以功能形式进行发送) - send mail using a php script(i want to make it in a form of function) 当我使用php邮件功能发送HTML电子邮件时,电子邮件(除gmail)未以正确的格式显示 - html email is not displaying in proper format in email (except gmail) when I send it using php mail function 使用php邮件功能发送电子邮件。 如果我从电子邮件中删除图像,则邮件已成功传递 - Send email using php mail function. Mail is successfully delivered if i remove image from email 无法使用Xampp从gmail发送邮件(使用php脚本) - Unable to send mail from gmail with xampp (using php script) 我可以使用gmail帐户发送邮件,但是不能使用laravel 4从我的邮件主机发送邮件? - I can send from using gmail account but can't send mail using laravel 4 form my mail host? 如何通过邮件()使用Gmail发送电子邮件? 我在哪里输入密码? - How do I Send email using Gmail through mail() ? Where do I put the password? 使用LOCALHOST smtp GMAIL上的XAMPP和PHP中的mail()函数发送邮件 - Send mail using mail() function in PHP , XAMPP on LOCALHOST smtp GMAIL 在本地主机中使用 php 使用 Gmail 发送邮件 - Send mail using Gmail from php in localhost 无法使用 PHP 邮件将数据从 HTML 表单发送到 email - Unable to send data from HTML form to email using PHP mail()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM