Home – Download › Forums › PHP › PHP mail() function not working – [Solution]
- This topic has 0 replies, 1 voice, and was last updated 5 years, 3 months ago by admin.
Viewing 0 reply threads
-
AuthorPosts
-
-
August 8, 2019 at 12:31 pm #264adminKeymaster
PHP mail() function not working is a common issue. A better solution is using the SMTP library. phpmailer is good SMTP mail library.
For testing purpose just create a file and add the following code.
123456789101112$subject = "Test mail";$message = "some php test message";$headers = 'From:' . $sender;if (mail($recipient, $subject, $message, $headers)){echo "Message accepted";}else {echo "mail function is not working";}Most of the shared web server is blocking mail() function to reduce spam emails. You can see commonly mail() function emails are going to spam or junk folder.
Mail function Details:-
mail ( string
$to
, string$subject
, string$message
[, mixed$additional_headers
)It will return boolean (true or false)
How to configure SMTP library ?
Install phpmailer library using composer. First install composer, if you not installed in your system.
1composer require phpmailer/phpmailer12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849<span class="pl-pse"><?php</span>require 'vendor/autoload.php';<span class="pl-s1"><span class="pl-c">// Import PHPMailer classes into the global namespace</span></span><span class="pl-s1"><span class="pl-c">// These must be at the top of your script, not inside a function</span></span><span class="pl-s1"><span class="pl-k">use</span> <span class="pl-c1">PHPMailer\PHPMailer\PHPMailer</span>;</span><span class="pl-s1"><span class="pl-k">use</span> <span class="pl-c1">PHPMailer\PHPMailer\Exception</span>;</span><span class="pl-s1"><span class="pl-c">// Load Composer's autoloader</span></span><span class="pl-s1"><span class="pl-k">require</span> <span class="pl-s"><span class="pl-pds">'</span>vendor/autoload.php<span class="pl-pds">'</span></span>;</span><span class="pl-s1"><span class="pl-c">// Instantiation and passing `true` enables exceptions</span></span><span class="pl-s1"><span class="pl-smi">$mail</span> <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-c1">PHPMailer</span>(<span class="pl-c1">true</span>);</span><span class="pl-s1"><span class="pl-k">try</span> {</span><span class="pl-s1"> <span class="pl-c">//Server settings</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">SMTPDebug</span> <span class="pl-k">=</span> <span class="pl-c1">2</span>; <span class="pl-c">// Enable verbose debug output</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>isSMTP(); <span class="pl-c">// Set mailer to use SMTP</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">Host</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>smtp1.example.com;smtp2.example.com<span class="pl-pds">'</span></span>; <span class="pl-c">// Specify main and backup SMTP servers</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">SMTPAuth</span> <span class="pl-k">=</span> <span class="pl-c1">true</span>; <span class="pl-c">// Enable SMTP authentication</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">Username</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>; <span class="pl-c">// SMTP username</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">Password</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>secret<span class="pl-pds">'</span></span>; <span class="pl-c">// SMTP password</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">SMTPSecure</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>tls<span class="pl-pds">'</span></span>; <span class="pl-c">// Enable TLS encryption, `ssl` also accepted</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">Port</span> <span class="pl-k">=</span> <span class="pl-c1">587</span>; <span class="pl-c">// TCP port to connect to</span></span><span class="pl-s1"> <span class="pl-c">//Recipients</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>setFrom(<span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>Mailer<span class="pl-pds">'</span></span>);</span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addAddress(<span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>Joe User<span class="pl-pds">'</span></span>); <span class="pl-c">// Add a recipient</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addAddress(<span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>); <span class="pl-c">// Name is optional</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addReplyTo(<span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>Information<span class="pl-pds">'</span></span>);</span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addCC(<span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>);</span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addBCC(<span class="pl-s"><span class="pl-pds">'</span>[email protected]<span class="pl-pds">'</span></span>);</span><span class="pl-s1"> <span class="pl-c">// Attachments</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addAttachment(<span class="pl-s"><span class="pl-pds">'</span>/var/tmp/file.tar.gz<span class="pl-pds">'</span></span>); <span class="pl-c">// Add attachments</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>addAttachment(<span class="pl-s"><span class="pl-pds">'</span>/tmp/image.jpg<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>new.jpg<span class="pl-pds">'</span></span>); <span class="pl-c">// Optional name</span></span><span class="pl-s1"> <span class="pl-c">// Content</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>isHTML(<span class="pl-c1">true</span>); <span class="pl-c">// Set email format to HTML</span></span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">Subject</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>Here is the subject<span class="pl-pds">'</span></span>;</span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">Body</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>This is the HTML message body <b>in bold!</b><span class="pl-pds">'</span></span>;</span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">AltBody</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">'</span>This is the body in plain text for non-HTML mail clients<span class="pl-pds">'</span></span>;</span><span class="pl-s1"> <span class="pl-smi">$mail</span><span class="pl-k">-></span>send();</span><span class="pl-s1"> <span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">'</span>Message has been sent<span class="pl-pds">'</span></span>;</span><span class="pl-s1">} <span class="pl-k">catch</span> (<span class="pl-c1">Exception</span> <span class="pl-smi">$e</span>) {</span><span class="pl-s1"> <span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>Message could not be sent. Mailer Error: {<span class="pl-smi">$mail</span><span class="pl-k">-></span><span class="pl-smi">ErrorInfo</span>}<span class="pl-pds">"</span></span>;</span><span class="pl-s1">}</span>Just post your doubts here.
-
-
AuthorPosts
Viewing 0 reply threads
- You must be logged in to reply to this topic.