PHP uses Gmail to send emails

Source: Internet
Author: User
Tags php class word wrap

The lab is going to hold a meeting to make me a website for a meeting. The use of open source openconf, very convenient. But the mail simple use of PHP mail function, without the SMTP authentication, send the mail is likely to appear in the other's trash, or even be sent back letter. Searched for a bit. There are many PHP class libraries that support the use of SMTP to send mail, such as Phpmailer.

The following is a function implemented using Phpmailer and a simple test code. "Note that running this function requires the openconf of class.phpmailer.php and class.smtp.phpunder the source code root directory."

Please replace "yourgmail@gmail.com" with your Gmail and write the corresponding password. Change "test@qq.com" to the mailbox you want to test.

<?php//below defines a function for sending mail that has been tested through. $sendto _email: Mailing Address//$subject: Mail Subject//$body: Message body//$sendto _name The name of the recipient, the name of the sender.  
Generally can be saved. function Stmp_mail ($sendto _email, $subject = null, $body = NULL, $sendto _name = null) {//vendor ("phpmailer.class# Phpmailer ");

    Import function Package class class.phpmailer.php require_once ("class.phpmailer.php"); $mail = new Phpmailer (); Create a new Message Send class object $mail->issmtp (); Send via SMTP $mail->port = 25; Send port $mail->host = "ssl://smtp.gmail.com:465"; SMTP mail server address, where you need to replace the mail server address where the mail is sent, using Gmail's SMTP settings $mail->smtpauth = true; Turn on SMTP authentication mail server Authentication open $mail->username = "yourgmail@gmail.com"; The user name of this mailbox on the SMTP server is only required for the previous part of the @, and some require full name. Please replace it with the correct mailbox username $mail->password = "Hu Jintao"; Password for this mailbox on the SMTP server, replace it with the correct password $mail->from = "yourgmail@gmail.com";                  
    The mailbox on the SMTP server that sends this message, replace it with the correct mailbox, $mail->username value is the corresponding. $mail->fromname = "YourName"; The name of the real sender and so on, as requiredTo fill in $mail->charset = "Utf-8";   
    This specifies the character set. 
    $mail->encoding = "base64"; $mail->addaddress ($sendto _email, $sendto _name); The recipient's mailbox and name//$mail->addreplyto (' yourgmail@gmail.com ', ' admin ');//This one is based on need//$mail->wordwrap = 50; Set word wrap//$mail->addattachment ("/var/tmp/file.tar.gz");   
    Annex Processing//$mail->addattachment ("/tmp/image.jpg", "new.jpg"); $mail->ishtml (TRUE); Send As HTML $mail->subject = $subject; Mail subject//mail content/* $mail->body = "Save the above code as gmail.php and write the following main.php
<?php
require_once "gmail.php";


echo "Send mail Begin";
echo "";
if (Stmp_mail ("your@qq.com", "subject", "Body"))
echo "Success";
Else
echo "failed";
echo "";
echo "Send mail End";


? >

Use the browser to access the mail.php test.


______________updated 2013.04.27___________________________________

The following city openconf changes to be made:

Place the above file gmail.php in the OPENCOF root directory to modify include.php in <. Add the following line of PHP

Require_once "gmail.php";  
					
Replace function Oc_mail:
function Oc_mail ($to, $subject, $body, $hdr = ', $enc = ' quoted-printable ') {
global $OC _configar;
$headers = (Empty ($HDR) $OC _configar[' oc_mailheaders ']: $HDR);
$headers = "mime-version:1.0\r\n". $headers. "\r\ncontent-type:text/plain; Charset=utf-8\r\ncontent-transfer-encoding: ".        $enc;
$headers = Preg_replace ("/\r/", "", $headers);
Return (Stmp_mail ($to, $subject, $body));
    


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.