All the programming languages provide functions for sending emails. PHP also provides mail() function to send emails. But the main point is to increase email delivery to inbox successfully. While sending simple emails the chances of getting email spam increase. Also, a simple PHP mail function has limited options for mail formatting and sending properly. To increase email deliverability and speed we are going to use PHPMailer, which providers lots of option for sending emails. It also provides an option to send email from SMTP servers.
Step 1 – Setup PHPMailer
Install the phpmailer module using composer under your application.
composer require phpmailer/phpmailer
Step 2 – Sending Emails using PHP and PHPMailer
Now create an simple php script sendMail.php in your web document root and add below content. Below script is using Gmail smtp server for sending mails. You may use any other SMTP server like Amazon SES, Sendgrid, Mailchimp or Mandril App etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = 2; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Password = '_password_'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; //Attachments $mail->addAttachment('/backup/myfile.tar.gz'); //Content $mail->isHTML(true); $mail->Subject = 'Test Mail Subject!'; $mail->Body = 'This is SMTP Email Test'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } |
Now access this application using a browser with your domain name following by script name
http://example.com/sendMail.php
9 Comments
I am also receiving the same blank screen when i run the sendMail.php. The apache error log entry is
“GET /sendMail.php HTTP/1.1” 500 185 “-” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0”
Simple instruction with good example code. Sending attachments is now easy. Thanks you!
It is also possible to use PHPMailer to send emails from local web server. For this you will have to define following variables
$mail->From
$mail->FromName
$mail->addAddress
$mail->isHTML(true)
$mail->Subject
$mail->Body
$mail->AltBody
Source: How to use PHPmailer
this line writen in my page when i go to
http://mydomain/sendmail.php
” b0nano 2.5.3xvrootvultr.ubuntusendmail.php ”
with no mail sent to mailbox i input in the sendmail.php
so. whats next and thank you. sir
Why I need this myfile.tar.gz? It’s making and error. I suppose it is because my server is Windows, but I don’t know what to do anyway..
“Could not access file: /backup/myfile.tar.gz
A mensagem não pode ser enviada.Mailer Error: Could not access file: /backup/myfile.tar.gz”
Hi Pachak,
I have setup php 7 and try to use phpmailer 5.2 version. But its provide following error :
Connection: opening to host(this is my email host):465
Mailer Error: SMTP connect() failed.
I have used 465,25 and 587.
Is phpmailer 5.2 not compatible with PHP 7?
Looking for your help.
Best,
Amit
Same for me
I tried with your code, but it returned blank page. It didn’t show “Message sent” nor any errors.
Hi,
Is there any error in Apache logs ?