简体   繁体   English

使用 PHP 向 gmail 帐户发送电子邮件

[英]Sending Email to gmail account using PHP

I am trying to send mail to gmail account from my web site.我正在尝试从我的网站向 gmail 帐户发送邮件。 It seems that everything is fine at the code level.似乎在代码级别一切都很好。 Can you guys please look into this issue: even if i run this script, mail is not received to gmail Account.你们能不能看看这个问题:即使我运行这个脚本,也没有收到邮件到 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';
            }

        ?>

I think you will install phpmailer( http://pear.php.net/ ) and then send through smtp here is example code:我想你会安装 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