簡體   English   中英

使用 PHP 向 gmail 帳戶發送電子郵件

[英]Sending Email to gmail account using PHP

我正在嘗試從我的網站向 gmail 帳戶發送郵件。 似乎在代碼級別一切都很好。 你們能不能看看這個問題:即使我運行這個腳本,也沒有收到郵件到 gmail 帳戶。

 <?php
        if(isset($_POST['submit'])){
        ini_set('SMTP','localhost'); 
        $msg='Name : '.$_POST['name']."\n"
                .'Email : '.$_POST['email']."\n"
                .'Message : '.$_POST['message'];

                mail("aa@gmail.com","Message from Contact Us",$msg);
                     }

        else{
               echo 'cannot send email';
            }

        ?>

我想你會安裝 phpmailer( http://pear.php.net/ ) 然后通過 smtp 發送這里是示例代碼:

require_once "Mail.php";

$from = '<from@gmail.com>';
$to = '<to@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'your gmail ',
        'password' => 'your password'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM