phpmailer脚本发送邮件

php脚本发送邮件。这个话题貌似已经被写滥了。自己亲手实践一下。做一个记录。


官网地址是

http://phpmailer.worxware.com/


到git上下载

https://github.com/PHPMailer/PHPMailer



#cd PHPMailer/

# ls -alhc

total 248K

drwxr-xr-x 8 root root 4.0K Aug 27 10:54 .

drwxr-xr-x 3 root root 4.0K Aug 27 10:54 ..

-rw-r--r-- 1 root root  22K Aug 27 10:54 changelog.md

-rw-r--r-- 1 root root  95K Aug 27 10:54 class.phpmailer.php

-rw-r--r-- 1 root root  11K Aug 27 10:54 class.pop3.php

-rw-r--r-- 1 root root  33K Aug 27 10:54 class.smtp.php



#!/usr/bin/php

<?php


array_shift($_SERVER['argv']);

if(count($_SERVER['argv']) != 3){

echo "Usage:php_mailer.php MAIL_TO SUBJECT MAINBODY";

exit;

}

#echo $_SERVER['argv'][0].chr(10);


$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";

if(!preg_match($pattern,$_SERVER['argv'][0])){

echo "not emailaddress".chr(10);

exit;

}

$mail_to=$_SERVER['argv'][0];

$mail_subject=$_SERVER['argv'][1];

$mail_body=$_SERVER['argv'][2];


function php_send_mail($mail_to,$mail_subject='',$mail_body=''){


require_once("./PHPMailer/class.phpmailer.php");

require_once("./PHPMailer/class.smtp.php");


$mail=new PHPMailer();

$mail->Charset = "UTF-8";


$mail->IsSmTP();

$mail->SMTPDebug  = 1;


$mail->SMTPAuth = true;

$mail->SMTPSecure = "ssl";

$mail->Host       = "smtp.gmail.com";

$mail->Port       = 465;


$mail->Username   = "[email protected]";

$mail->Password   = "密码";

$mail->SetFrom('[email protected]', 'My gmail');

$mail->AddReplyTo("[email protected]","yzhkpli");


$mail->IsHTML = true;

$mail->Subject    = $subject;

$mail->Body =  eregi_replace("[\]",'',$mail_body);

$mail->AltBody    = "为了查看该邮件,请切换到支持 HTML 的邮件客户端";

$mail->MsgHTML($mail->Body);

#$address=$to

$mail->AddAddress($mail_to,'my qq');

if(!$mail->Send()) {

echo "发送失败:" . $mail->ErrorInfo;

exit;

} else {

echo "恭喜,邮件发送成功!";

}

}

php_send_mail($mail_to,$mail_subject,$mail_body);

?>


你可能感兴趣的:(PHP,phpmailer)