PHP using Phpmailer to send mail _php tips

Source: Internet
Author: User
Tags word wrap

A local server that does not send mail is a good choice to send mail with a ready-made SMTP server, the tool used here is Phpmailer (Version 5.2.0), and the SMTP server chooses Gmail and 163.

1. Use Gmail to send the script

Include ("class.phpmailer.php");
Include ("class.smtp.php");
Gets the contents of an external file $mail = new Phpmailer ();
$body = file_get_contents (' contents.html ');
$body = eregi_replace ("[\]", ", $body);
Set SMTP Parameters $mail->issmtp ();
$mail->smtpauth = true;
$mail->smtpkeepalive = true;
$mail->smtpsecure = "SSL";
$mail->host = "smtp.gmail.com";
$mail->port = 465;
Fill in your Gmail account and password $mail->username = "yourname@gmail.com";
$mail->password = "Password";
Set the sender, it is best not to forge the address $mail->from = "yourname@gmail.com";
$mail->fromname = "Webmaster";
$mail->subject = "This is the Subject";
$mail->altbody = $body; $mail->wordwrap = 50;
Set word wrap $mail->msghtml ($body);
Set the reply address $mail->addreplyto ("yourname@gmail.com", "webmaster");
Add attachments, where the attachment is in the same directory as the script//otherwise fill out the full path $mail->addattachment ("attachment.jpg");
$mail->addattachment ("Attachment.zip");
Set the Mail recipient's mailbox and name $mail->addaddress ("toname@gmail.com", "FirstName LastName"); Send messages in HTML format $mail->ishtml (TRUE); Send mail via Send///////According to the result of delivery if (! $mail->send ()) {echo "Mailer Error:".
$mail->errorinfo;
 else {echo "message has been sent";}

2. Use 163 script to send mail

You only need to change the SMTP configuration and account password, SMTP configuration is as follows

Set SMTP parameters
//Note there is no need for SSL protocol
$mail->ISSMTP ();
$mail->smtpauth  = true;
$mail->smtpkeepalive = true;
$mail->host    = "smtp.163.com";
$mail->port    = 25;

Testing through in a local wampserver environment requires opening the PHP_OPENSSL extension.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.