I have mail function in php which looks like:
<?php
$admin_email = "admin#gmail.com";
$email ="myemail#gmail.com";
$subject = "hellow ord";
$comment = "cmt";
try{
$th=mail($admin_email, $subject, $comment, "From:" . $email);
if($th)
echo "gg";
else
echo "error_message";
}
catch(Exception $e)
{
echo $e->message();
}
?>
I have Wamp server to run it. I have configured hMailServer by seeing a post on Stack Overflow but when I run the above code I get:
Warning: mail(): SMTP server response: 530 SMTP authentication is required...
Do I need to set up anything before using mail function?
The PHP mail() function doesn't support smtp authentication, which generally results in an error when connecting to public servers like the one you are trying to connect to. With hmailserver installed, you still need to use a library like PHPMailer which you can autheticate with.
Follow the steps here to setup phpmailer with gmail:
http://blog.techwheels.net/tag/wamp-server-and-phpmailer-and-gmail/
I've successfully sent email using gmail on (wamp/hmailserver/phpmailer).
<?php
require_once ("class.phpmailer.php");
$sendphpmail = new PHPMailer();
$sendphpmail->IsSMTP();
$sendphpmail->SMTPAuth = true;
$sendphpmail->SMTPSecure = "ssl";
$sendphpmail->Host = "smtp.gmail.com";
$sendphpmail->Port = 465;
$sendphpmail->Username = "your#gmail.com";
$sendphpmail->Password = "gmail_password";
$sendphpmail->SetFrom('your#gmail.com','blahblah');
$sendphpmail->FromName = "From";
$sendphpmail->AddAddress("recipient#gmail.com"); //don't send to yourself.
$sendphpmail->Subject = "Hello!";
$sendphpmail->Body = "<H3>Check out www.google.com</H3>";
$sendphpmail->IsHTML (true);
if (!$sendphpmail->Send())
{
echo "Error: $sendphpmail->ErrorInfo";
}
else
{
echo "Message Sent!";
}
?>
Hope this helps others as well. Took me a few hours of googling and compiling answers from places while facing this issue.
Related
I am using PHPMailer with SMTP to send email. First I just test it from my localhost and it's successfully sent email.
But when I upload the same code to my service it's showing me following error without sending email;
Error Message is :
2016-04-22 05:53:10 SMTP ERROR: Failed to connect to server: Connection refused (111) 2016-04-22 05:53:10 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting mail is not send
I do not understand why it's showing me this error message becuase I am using 100% same code.
Here is my email sending script using PHPMailer :
require 'PHPMailerAutoload.php';
$m = new PHPMailer();
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 2;
$m->Host = 'smtp.gmail.com';
$m->Username = 'username';
$m->Password = 'password'; // google app password
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->From = 'from#gmail.com';
$m->FromName = 'Shibbir Ahmed';
$m->addReplyTo('reply#gmail.com', 'Reply Address');
$m->addAddress('to#gmail.com', 'Shibbir Ahmed');
$m->Subject = 'Here is an email';
$m->Body = 'This is email';
$m->AltBody = 'Alt body';
if($m->send()) {
echo 'mail send';
} else {
echo 'mail is not send';
}
Some servers have certain functions disable that a user cannot enable them selves. Check with your hosting provider to see if they have the mail() function enabled.
In this case smtp mail function perfectly working in localhost but not in server..
soo you change the server setting
That link to the troubleshooting guide is there for a reason - if you follow it it will tell you exactly what is wrong: GoDaddy blocks outbound SMTP, so you have to use their mail servers. There are many duplicates of this question, so search before you post.
I am using PHPMailer to send emails notification from our internal stmp server in the company. The emails are sent successfully but not stored in the sent Items folder on Outlook.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtpinternal.xxxx.com';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->Username = 'xxxxnoreply#xxxx.com';
$mail->Password = 'xxxx';
$mail->Port = 25;
$mail->setFrom('xxxxnoreply#xxxx.com', 'xxxx_NoReply');
$distributionLists = explode(',', $items['Distribution List']);
foreach ($distributionLists as $distributionList) {
if (!empty($distributionList) && strpos( $distributionList, '#' ) !== false ) {
$mail->addAddress(trim($distributionList)); }
}
$contacts = explode(',', $items['Contact']);
foreach ($contacts as $contact) {
if (!empty($contact) && strpos( $contact, '#' ) !== false ) {
$mail->addCC(trim($contact)); }
}
$mail->isHTML(true);
$mail->Subject = 'Invoice Report';
$mail->Body = "Dear Client, <br><br> Please find below today's invoices <br><br>". $table ."<br> Please contact your representative on Email address : ". $items['Contact'] ."<br><br> Best regards. <br><br> Please do not reply to this message, as it has been sent by a system email address.";
$mail->send();
$mail_string = $mail->getSentMIMEMessage();
$path = "{".$mail->Host.":25/imap/ssl/novalidate-cert}";
$imapStream = imap_open($path, $mail->Username, $mail->Password);
imap_append($ImapStream, $path, $mail_string, "\\Seen");
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }
?>
The imap_open function returns the following error :
imap_open(): Couldn't open stream {smtpinternal.xxxx.com:25/imap/ssl/novalidate-cert}
I tried updating $path to
{"imap.smtpinternal.xxxx.com:25/imap/ssl/novalidate-cert} or {smtpinternal.xxxx.com:25/imap/ssl/authuser=xxxxnoreply#xxxx.com}
But still getting the same error. Any suggestions please how i should declare my path variable to point to Sent Items folder in Outlook ? Thank you.
I'd guess the problem is that you are trying to use IMAP on port 25, which is inbound SMTP's port. You should be using port 143 for IMAP, or 993 if you want TLS encryption on it, which is what the IMAP upload example provided with PHPMailer does.
I can't tell you what IMAP paths to use – you would need to refer to Outlook's docs for more on that.
The latest versions of Exchange (such as those used by Office 365) already save messages sent through SMTP in the user's Sent Items folder.
So I have a contact form that uses phpmailer. It sends an email from one Gmail account to another. But I can't seem to get the receiving email to receive any emails.
The script is hosted on a cpanel (RivalHost) and the domain is on GoDaddy. I asked RivalHost if they were blocking SMTP connections or blocking ports 587 or 465, and they said they weren't. So I have no idea what's causing the issue. The script works perfectly fine on my localhost, just not on cpanel
Heres the mailing script:
<?php
$result="";
if(isset($_POST['submit'])){
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host='smtp.gmail.com';
$mail->Port=465;
$mail->SMTPAuth=true;
$mail->SMPTSecure='ssl';
$mail->Username='sendingemail#gmail.com';
$mail->Password='*********';
$mail->setFrom('sendingemail#gmail.com');
$mail->addAddress('receivingemail#gmail.com');
$mail->addReplyTo($_POST['email'],$_POST['name']);
$mail->isHTML(true);
$mail->Subject='Contact: '.$_POST['subject'];
$mail->Body='Message: '.$_POST['msg'].'</h1>';
if(!$mail->send()){
$result='something went wrong';
echo $result;
} else {
$result="thank you";
echo $result;
}
}
?>
I was also told to check my MX records, but wasn't really sure what to change them to, or if I needed to change them at all:
MX 0 ********.com 3599 RBL
Solution 1:
PHPMailer uses exeptions. You can put your code in try/catch block to get exceptions caused emails not to be sent.
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
//Email information comes here
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
Solotion 2:
Are you also using the CSF firewall? If so, check to see if the "SMTP_BLOCK" setting is enabled. If STMP_BLOCK is enable contact to hosting to disable it.
Add this to your settings:
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
I am attempting to use PhpMailer to send an email of an MP3 to a user. When I try to send an email to the user I get a message saying: SMTP ERROR: Failed to connect to server: No route to host (65) . I have read this might be the result of gmail not agreeing with a different server but I don't see any remedy. I switched from tls to ssl and that didn't help. I also tried 3 different physical locations and the problem still persists. This worked fine initially and maybe something switched off but I don't know what. UPDATE I am having this problem only on my hosted site . localhost is working now. Is there some necessary configuration on my shared server I am missing? Any ideas would be greatly appreciated.
<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
date_default_timezone_set('America/New_York');
require_once 'vendor/autoload.php';
$m = new PHPMailer(true);
try{
$m->SMTPDebug = 2;
$m->isSMTP();
$m->Host = 'smtp.gmail.com';
$m->SMTPAuth = true;
$m->Username = 'munsonatl#gmail.com';
$m->Password = 'somethingsecret';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->setFrom('munsonatl#gmail.com', 'Mailer');
$m->addAddress('munsonatl#gmail.com', 'Matt Macy');
$m->addReplyto('reply#mattmacy.com', 'replyAddress');
require 'connect.php';
$itemNum = $_SESSION['itemNum'];
$query2 = "SELECT* FROM MP3s_For_Sale WHERE itemNum = :itemNum";
$LastProduct = $db->prepare($query2);
$LastProduct->bindvalue(':itemNum', $itemNum);
$LastProduct->execute();
$rows = $LastProduct->fetch();
$filename = $rows['path'];
$filesize = $rows['filesize'];
$string = $rows['wholeMP3'];
$encoding = 'base64';
$type = $rows['type'];
$m->AddStringAttachment($string,$filename,$encoding,$type);
$m->isHTML(true);
$m->Subject = "Here is an Email";
$m->Body = "<p>This is the body of the email</p><br><strong>Test for
HTML formatting</strong><br>";
$m->AltBody = "This is the body of an email";
$m->send();
echo "message has been sent";
unset($_SESSION['itemNum']);
} catch (Exception $e){
echo "message could not be sent", $m->ErrorInfo;
}
?>
I don't have the reputation to leave a comment like the others did, but I have had this issue before caused by the port being blocked on shared hosting. If you need to check if yours is blocked, then look at PHP Checking if a port is Active
After talking with my hosting company for the third time they said web.com does not require the use of ports or even SMTP to work with PhpMailer. I commented out everything that has to do with Ports or SMTP and as a result it now works kind of. I haven't been able to send email to my secondary email account at all and it works only most of the time with my gmail account. If someone can tell me anything else about web.com and what is going on it would be very helpful.
I am using PHPMailer with SMTP to send email. First I just test it from my localhost and it's successfully sent email.
But when I upload the same code to my service it's showing me following error without sending email;
Error Message is :
2016-04-22 05:53:10 SMTP ERROR: Failed to connect to server: Connection refused (111) 2016-04-22 05:53:10 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting mail is not send
I do not understand why it's showing me this error message becuase I am using 100% same code.
Here is my email sending script using PHPMailer :
require 'PHPMailerAutoload.php';
$m = new PHPMailer();
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 2;
$m->Host = 'smtp.gmail.com';
$m->Username = 'username';
$m->Password = 'password'; // google app password
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->From = 'from#gmail.com';
$m->FromName = 'Shibbir Ahmed';
$m->addReplyTo('reply#gmail.com', 'Reply Address');
$m->addAddress('to#gmail.com', 'Shibbir Ahmed');
$m->Subject = 'Here is an email';
$m->Body = 'This is email';
$m->AltBody = 'Alt body';
if($m->send()) {
echo 'mail send';
} else {
echo 'mail is not send';
}
Some servers have certain functions disable that a user cannot enable them selves. Check with your hosting provider to see if they have the mail() function enabled.
In this case smtp mail function perfectly working in localhost but not in server..
soo you change the server setting
That link to the troubleshooting guide is there for a reason - if you follow it it will tell you exactly what is wrong: GoDaddy blocks outbound SMTP, so you have to use their mail servers. There are many duplicates of this question, so search before you post.