· 9 years ago · Apr 05, 2017, 02:18 PM
1<?php
2// This example shows settings to use when sending via Google's Gmail servers.
3//
4
5//SMTP needs accurate times, and the PHP time zone MUST be set
6//This should be done in your php.ini, but this is how to do it if you don't have access to that
7date_default_timezone_set('Etc/UTC');
8
9require 'PHPMailer/PHPMailerAutoload.php';
10
11//Create a new PHPMailer instance
12$mail = new PHPMailer;
13
14//Tell PHPMailer to use SMTP
15$mail->isSMTP();
16
17//Enable SMTP debugging
18// 0 = off (for production use)
19// 1 = client messages
20// 2 = client and server messages
21$mail->SMTPDebug = 2;
22
23//Ask for HTML-friendly debug output
24$mail->Debugoutput = 'html';
25
26//Set the hostname of the mail server
27$mail->Host = 'smtp.gmail.com';
28// use
29// $mail->Host = gethostbyname('smtp.gmail.com');
30// if your network does not support SMTP over IPv6
31
32//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
33$mail->Port = 587;
34
35//Set the encryption system to use - ssl (deprecated) or tls
36$mail->SMTPSecure = 'tls';
37
38//Whether to use SMTP authentication
39$mail->SMTPAuth = true;
40
41//Username to use for SMTP authentication - use full email address for gmail
42$mail->Username = "*******@gmail.com";
43
44//Password to use for SMTP authentication
45$mail->Password = "+++++++++";
46
47//Set who the message is to be sent from
48$mail->setFrom('no-reply@midominio.com', 'No responder');
49
50//Set an alternative reply-to address
51//$mail->addReplyTo('replyto@example.com', 'First Last');
52
53//Set who the message is to be sent to
54$mail->addAddress('*********@gmail.com', 'Nombre Destinatario');
55
56//Set the subject line
57$mail->Subject = 'PHPMailer GMail SMTP test';
58
59$nombre='Federico Lorca';
60//Read an HTML message body from an external file, convert referenced images to embedded,
61//convert HTML into a basic plain-text alternative body
62$mail->Body = "<h1>Prueba de correo</h1><br><br>Mensaje de prueba enviado por {$nombre} con phpmailer en formato html<br>";
63
64//Replace the plain text body with one created manually
65$mail->AltBody = 'This is a plain-text message body';
66
67//Attach an image file
68//$mail->addAttachment('images/phpmailer_mini.png');
69
70//send the message, check for errors
71if (!$mail->send()) {
72 echo "Mailer Error: " . $mail->ErrorInfo;
73 } else {
74 echo "Message sent!";
75} ?>