Advertisement
robthebin

PHPMailer Sample

Oct 16th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\Exception;
  4.  
  5. require 'vendor/autoload.php';
  6.  
  7. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  8. try {
  9. //Server settings
  10. $mail->SMTPDebug = 2; // Enable verbose debug output
  11. $mail->isSMTP(); // Set mailer to use SMTP
  12. $mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
  13. $mail->SMTPAuth = true; // Enable SMTP authentication
  14. $mail->Username = 'user@example.com'; // SMTP username
  15. $mail->Password = 'secret'; // SMTP password
  16. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  17. $mail->Port = 587; // TCP port to connect to
  18.  
  19. //Recipients
  20. $mail->setFrom('robthebin@hotmail.com', 'Mailer');
  21. $mail->addAddress('robthebin@gmail.com', 'Test User'); // Add a recipient
  22. $mail->addAddress('ellen@example.com'); // Name is optional
  23. $mail->addReplyTo('info@example.com', 'Information');
  24. $mail->addCC('cc@example.com');
  25. $mail->addBCC('bcc@example.com');
  26.  
  27. //Attachments
  28. $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
  29. $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
  30.  
  31. //Content
  32. $mail->isHTML(true); // Set email format to HTML
  33. $mail->Subject = 'Here is the subject';
  34. $mail->Body = 'This is the HTML message body <b>in bold!</b>';
  35. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  36.  
  37. $mail->send();
  38. echo 'Message has been sent';
  39. } catch (Exception $e) {
  40. echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
  41. }
  42.  
  43. if ($_SERVER['REQUEST_METHOD']=='POST') {
  44. $emails = $_POST["emails"];
  45. }
  46. ?>
  47.  
  48. <form method="POST" action="">
  49. <input type="text" name="emails">
  50. <input type="submit">
  51. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement