Common methods of sending mail in PHP

Source: Internet
Author: User
Tags set time time interval email account port number

Using the PHP mail function to send

To send a message using the mail () function, you must have an SMTP server that does not need to be authenticated.

So the configuration work will be a little more, but the use of the time is more convenient, a few lines of code can be

The code is as follows Copy Code

$to = "test@163.com";
$subject = "Test";
$message = "This is a test mail!";
Mail ($to, $subject, $message);

JMail Send mail

Account must have SMTP Write permission, it is best to set $jmail->silent=true, do not let JMail throw the error message.
Commonly used have NetEase, Sogou, QQ, Gmail mailbox.

The code is as follows Copy Code

<?php
$jmail = new COM (' Jmail.message ') o& #114; Die (' cannot invoke JMail component ');
$jmail->silent = true; Mask exception Error
$jmail->charset = ' gb2312 '; Otherwise Chinese will be garbled
$jmail->from = ' web535000@163.com '; Here in theory can not be the same as the sender's address, but with 163 tried a few times can not, changed to the same on the
$jmail->fromname = ' bobotest ';
$jmail->addrecipient (' abc1@163.com '); Multiple message recipients can be added
$jmail->addrecipient (' abc2@163.com ');
$jmail->addrecipient (' abc3@163.com ');
$jmail->subject = ' email test. '
$jmail->body = ' This is test email. '
$jmail->mailserverusername = ' web535000@163.com '; Letter Email Account Number
$jmail->mailserverpassword = ' 123456 '; Password for the account
try{
$email = $jmail->send (' smtp.163.com ');
if ($email) echo ' sends success ';
else echo ' send failed ';
catch (Exception $e) {
echo $e->getmessage ();
}
?>

Letter account must have SMTP letter permission, for NetEase's mailbox, 06 years after the registration of a new mailbox does not seem to be, the early fall.
Also, it is best to set $jmail->silent=true, do not allow jmail to throw the wrong message, in the normal time, frequent letters will appear some errors, according to my estimate should be the letter server set the time interval.

Phpmailer Send mail, this is quite simple is also commonly used

The code is as follows Copy Code


function postmail_jiucool_com ($to, $subject = "", $body = "") {
Author:jiucool website:http://www.jiucool.com
$to represents the recipient address $subject represents the message header $body the message body
Error_reporting (E_all);
Error_reporting (e_strict);
Date_default_timezone_set ("Asia/shanghai");/set time zone East eight area
Require_once (' class.phpmailer.php ');
Include ("class.smtp.php");
$mail = new Phpmailer (); New a Phpmailer object out
$body = Eregi_replace ("[]", ", $body); Filter the content of the message as necessary
$mail->charset = "UTF-8";//Set message encoding, default iso-8859-1, if you have to set this item in Chinese, otherwise garbled
$mail->issmtp (); Set up to use the SMTP service
$mail->smtpdebug = 1; To enable the SMTP debugging feature
1 = errors and messages
2 = messages Only
$mail->smtpauth = true; To enable the SMTP authentication feature
$mail->smtpsecure = "SSL"; Security protocols
$mail->host = "smtp.googlemail.com"; SMTP Server
$mail->port = 465; Port number of the SMTP server
$mail->username = "SMTP Server user name"; SMTP Server user name
$mail->password = "SMTP server password"; SMTP server password
$mail->setfrom (' sender address, such as admin#jiucool.com #换成 @ ', ' Name of Sender ');
$mail->addreplyto ("Mail reply address, such as admin#jiucool.com #换成 @", "Mail reply person's name");
$mail->subject = $subject;
$mail->altbody = "To view", compatible email viewer! with an HTML -From Www.111cn.net "; Optional, comment out and test
$mail->msghtml ($body);
$address = $to;
$mail->addaddress ($address, "Recipient name");
$mail->addattachment ("Images/phpmailer.gif"); Attachment
$mail->addattachment ("Images/phpmailer_mini.gif"); Attachment
if (! $mail->send ()) {
echo "Mailer Error:". $mail->errorinfo;
} else {
echo "Message sent! Congratulations, Mail sent success!" ";
}
}

Main functional features of Phpmailer:

1. Digital signatures that support message S/MIME encryption
2. Support mail multiple TOs, CCs, BCCs and Reply-tos
3. Can work on any server platform, so don't worry about the win platform can not send mail problems
4. Support Text/html format mail
5. Can embed image images
6. Support for mail clients that do not support HTML reading
7. Powerful Send mail debug function debug
8. Custom message Header
9. Redundant SMTP server support
10. Support 8bit, base64, binary, and quoted-printable coding
11. Text Wrap
12. Support multi-attachment send function
13. Support SMTP Server Authentication function
14. Test success in SendMail, QMail, Postfix, Gmail, Imail, Exchange and other platforms
15. The download file provided, including detailed description of the document and sample instructions, so do not worry about difficult to use the problem!
16.PHPMailer is very small, simple, convenient, fast

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.