Example of using phpMailer to send mails in php
- Require ("phpmailer/class. phpmailer. php ");
- Function smtp_mail ($ sendto_email, $ subject, $ body, $ extra_hdrs, $ user_name ){
- $ Mail = new PHPMailer ();
- $ Mail-> IsSMTP (); // send via SMTP
- $ Mail-> Host = "smtp.163.com"; // SMTP servers
- $ Mail-> SMTPAuth = true; // turn on SMTP authentication
- $ Mail-> Username = "xuchao842363331"; // SMTP username note: for normal mail authentication, @ domain name is not required. here is my 163 mailbox.
- $ Mail-> Password = "password"; // enter the email password here
- $ Mail-> From = "jbxue123@163.com"; // sender's mailbox
- $ Mail-> FromName = "administrator"; // sender
-
- $ Mail-> CharSet = "UTF-8"; // specify the character set here! After the UTF-8 is specified, the Mail title and sender will not be garbled, and if it is GB2312 title will be garbled
- $ Mail-> Encoding = "base64 ";
- $ Mail-> AddAddress ($ sendto_email, "username"); // recipient's email address and name
- $ Mail-> AddReplyTo ("yourmail@yourdomain.com", "yourdomain.com ");
- // $ Mail-> WordWrap = 50; // set word wrap
- // $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // attachment
- // $ Mail-> AddAttachment ("/tmp/image.jpg", "new.jpg ");
- // $ Mail-> IsHTML (true); // send as HTML
- // Email subject
- $ Mail-> Subject = $ subject;
- // Email content
- $ Mail-> Body = "hello! PHPMailer ";
- // $ Mail-> AltBody = "text/html ";
- If (! $ Mail-> Send ())
- {
- Echo "error
";
- Echo "error:". $ mail-> ErrorInfo;
- Exit;
- }
- Else {
- Echo "success! ";
- }
- }
- // Parameter description (sent to, subject, content, additional information, user name)
- ?>
-
(When the character set is specified as GB2312 title will be garbled, here designated as UTF-8 will not appear garbled. In fact, PHPMailer has many other functions, such as adding attachments. we will not describe it here. This function can be called when the Mail function is required:
- Require ("mail. php ");
- Smtp_mail ("790896688@qq.com", "Welcome to the programmer's House", "username ");
- ?>
|