I'm trying to send an email using phpmailer, it works fine when I send to gmail but when i send to yahoo, hotmail and so on its not works.
Here is my code:
$mail = new PHPMailer;
$email->IsSMTP();
$email->Host = "smtp.yahoo.com";
$email->SMTPAuth = true;
$email->SMTPSecure = 'tls';
$email-> Port = 465;
$email->Username = "username";
$email->Password = "*******";
$email->SMTPDebug = 2;
$mail->CharSet = "UTF-8";
$mail->From = 'anonymous#domain.fr';
$mail->FromName = 'Mailer';
$mail->AddAddress('test#yahoo.com');
$mail->AddReplyTo('no-reply#domain.fr');
$mail->IsHTML(false);
$mail->Subject = $subject;
$mail->Body = $mail_msg;
if ( $email->send() ){
echo "success";
}
else {
echo "555 " .$email->ErrorInfo;
}
Some basic reading of the docs and example code would help. You can't use explicit TLS (SMTPSecure = 'tls') on a port expecting implicit TLS (Port = 465). Set Port = 587.
Of course you also need to have a working login for each service that you connect to, but the debug output will tell you if you've got that wrong.
Related
I am trying to send a fax via the eFax service. As I understand, this should be as easy as sending a simple email to the specific address.
I use PHPMailer for emails. Ordinary emails are sending fine. Even when I send to an efax address $mail->send returns true, that means that the email was sent seccessfully, but the recipient didn't receive the fax.
Any suggestions?
Thank you.
function sendFax($fax, $msg) {
$efax_number = $fax . "#efaxsend.com";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = EMAIL_HOST; // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
//From email address and name
$mail->From = "orders#orderapp.com";
$mail->FromName = "Test";
//To address and name
$mail->addAddress($efax_number); // SEND EMAIL TO USER
//Send HTML or Plain Text email
$mail->isHTML(false);
$mail->Subject = ' New Order Fax ';
$mail->Body = $msg;
$mail->AltBody = "Sample";
$result = $mail->send();
if (!$result)
{
...
}
else
{
...
}
}
Inconsistent duplicate emails occurring when using php mailer.
Function that mails:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require( GetPHPMailPath() );
$mail = new PHPMailer();
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!='') {
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->Username = "email#email.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "email"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
How I am calling the function:
echo SendEmail($toAddress,$ccAddress,$bccAddress,$subject,$body);
The really odd part about this whole ordeal is that it is inconsistent which means there may be nothing wrong with the code but the connection to gmail?
Any ideas maybe its a php.ini problem?
This was a lag related issue.
PHPMailer functioned properly. User was sending duplicate requests. Fixed by adding comparison check with MySQL database records.
I’m trying to make a contact form and I’m using PHPMailer. I tried that on localhost with xampp and it works perfect. But when i upload to my host i get the error SMTP connect() failed.
Here is my code:
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = "smtp.gmail.com";
$m->Username = "mymail#gmail.com";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";
$m->isHTML();
$m->Subject = "Hello world";
$m->Body = "Some content";
$m->FromName = "Contact";
$m->addAddress('mymail#gmail.com', 'Test');
I've tried to change the port to 587 and the SMTPsecure to tls (and all the combinations). But doesn’t work. Any advice to solve this?
Thanks
This answer work form me: https://stackoverflow.com/a/47205296/2171764
I use:
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You may need to specify the address from which the message is going to be sent, like this:
$mail->From = 'user#domain.com';
I would also give isHTML a parameter, either true or false:
$m->isHTML(true);
Another option is trying to drop the port specification all together. There are several other parameters that you may find useful. The following example is code I've tested, see if you can adapt it for your uses:
$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'mail.domain.com';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $username;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
/*$mail->SMTPSecure = 'tls';*//*Enable encryption, 'ssl' also accepted*/
$mail->From = 'user#domain.com';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('cc#example.com');*/
/*$mail->addBCC('bcc#example.com');*/
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(false);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header("Location: ../docs/confirmSubmit.html");
}
Hope this helps!
I am trying to setup PHPMailer for a customer. He has his own mail server located at a certain IP address. When asked to give me the information to send email through the system, he gave the following:
Host: xx.xxx.x.x
Port: 25
Domain: mydomain.local
Username: myemail#mydomain.local
Password: <myemailpassword>
From: myemail#anotherdomain.xx
(Which he confirmed is being used for external email sending)
I tried to setup PHPMailer by setting the parameters to the exact namings above.
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
I got the following error:
Failed to connect to server (0)
So I try to send an email through telnet to check if it's the customer's email server or the PHPMailer settings:
telnet xx.xxx.x.x 25
It goes through, I'm connected to the server.
helo mydomain.local
I'm getting 'Hello' as a reply. This leads me to believe it might be the PHPMailer settings that are wrong here.
I also try not using SMTP:
$mail->Host = "ssl://xx.xxx.x.x";
$mail->Port = 25;
$mail->Username = "myemail#mydomain.local";
$mail->Password = "password";
$mail->SetFrom('myemail#anotherdomain.xx', 'Webname');
$mail->[...]
Again no go. Am I going about this wrong? I'm only familiar with setting up PHPMailer to use Gmail before so I'm at a loss as to what could be the issue because I'm using a 'personal' email server.
Thanks Loadparts for your assistance.
I'm still not sure what the issue was but it seems it has resolved itself. It might have been from the email server side because coding wise, I didn't change anything. This is the final code I used.
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Host = "xx.xxx.x.x"; // SMTP server
$mail->Username = "myemail#mydomain.local";
$mail->Password = <myemailpassword>;
$mail->From = "myemail#anotherdomain.xx";
$mail->FromName = <Web_Name>;
$mail->AddAddress("email#domain.com");
$mail->Subject = <Subject>;
$mail->AltBody = <Alt_Body>
$mail->WordWrap = 80;
$body = "test message";
$mail->MsgHTML($body);
$mail->IsHTML(true);
$mail->Send();
I use a test function that I know works 100% to test the email servers when using PHPMailer.
I'm not sure why you are having your problem, but try to use the function I have ( I know it's messy but it does the trick). Just replace all the XXXX with your info and make sure you have both class.phpmailer.php and class.smtp.php in the same folder.
<?php
error_reporting(E_ALL);
$toemail = 'XXXX';
$toname = 'XXXX';
$subject = 'Testing Email Sending...';
$bodyhtml = '<H1>yeah</h1>';
$bodytext = 'heres Hoping it works';
$fromemail = 'XXXX';
$fromname = 'XXXX';
var_dump(sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname));
function sendemail($toemail,$toname,$subject,$bodyhtml,$bodytext,$fromemail,$fromname)
{
require_once("class.phpmailer.php");
$mail = new phpmailer();
$mail->IsSMTP();
$mail->From = $fromemail;
$mail->FromName = $fromname;
$mail->Host = "XXXX";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "XXXX"; // SMTP username
$mail->Password = "XXXX"; // SMTP password
$mail->Port="25";
$mail->SMTPDebug=true;
if(strlen($bodyhtml)>0) {
$mail->Body = $bodyhtml;
$mail->IsHTML(true);
}
else if(strlen($bodytext)>0){
$mail->Body = $bodytext;
}
if(strlen($bodytext)>0 && strlen($bodyhtml)>0){
$mail->AltBody = $bodytext;
}
$mail->AddReplyto($fromemail,$fromname);
$mail->Subject = $subject;
// Check if multiple recipients
if(preg_match("/;/",$toemail))
{
$tmp_email=preg_split("/;/",$toemail);
$tmp_contact=preg_split("/;/",$toname);
$mail->AddAddress($tmp_email[0], $tmp_contact[0]);
// echo "<!-- multi email:".$tmp_email[0]." contact:".$tmp_contact[0]." -->\n";
for($j=1;$j<count($tmp_email);$j++)
{
if(preg_match("/\#/",$tmp_email[$j]))
{ $mail->AddCC($tmp_email[$j], $tmp_contact[$j]);
// echo "<!-- multi email cc:".$tmp_email[$j]." contact:".$tmp_contact[$j]." -->\n";
}
}
}
else{
$mail->AddAddress($toemail, $toname);
}
$error= false;
if($mail->Send()){
$error =true;
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
return $error;
}
If this doesn't work, my first try would be using port 80 - which usually isn't blocked, then you can work on getting SSL to work.
PS: because it's a local domain, you may want to consider adding the domain to your /etc/hosts just to be sure.
Best of Luck!
I am failing to send email in my application hosted on appfog
i am using the following code which works fine on localhost but fail on appfog.
JPhpMailer extend class.PhpMailer.php
$mailer = new JPhpMailer(true);
$mailer->IsSMTP();
$mailer->Mailer = "smtp";
//$mailer->SMTPSecure == 'tls';
$mailer->Host = 'ssl://smtp.gmail.com';
$mailer->Port = '465';
$mailer->SMTPAuth = true;
//$mailer->SMTPSecure = true;
$mailer->Username = 'me#gmail.com';
$mailer->Password = 'zzzzzzz';
$mailer->SetFrom($to['from'], $to['from_name']);
$mailer->AddAddress($to['to'],$to['to_name'] );
$mailer->Subject = $to['subject'];
$mailer->Body = $to['body'];
$mailer->Send();
here is the line that in phpMailer that fails to execute`if ($tls) {
if (!$this->smtp->StartTLS()) {
throw new phpmailerException($this->Lang('tls'));
}
//We must resend HELO after tls negotiation
$this->smtp->Hello($hello);
}
$connection = true;
if ($this->SMTPAuth) {
if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
**strong text throw new phpmailerException($this->Lang('authenticate')); ** }
}
}
$index++;
if (!$connection) {
throw new phpmailerException($this->Lang('connect_host'));
}
The code below is working for me :
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = 587;
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "mypass"; // SMTP password
$mail->From = "myemail#gmail.com";
$mail->FromName = "myname";
$mail->AddAddress("myaddress#gmail.com", "myname");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
I encountered the same problem. To get it working, I had to go to myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON" (near the bottom of the page).
Hope it helps some one
After signing up to appfog, I was able to get PHPMailer working with the following.
I was unable to find JPHPMailer, although I suspect that isn't the cause of your issue but the fact that you were putting ssl://smtp.gmail.com as the host.
ini_set('display_errors', 1);
error_reporting(E_ALL);
include('class.phpmailer.php');
$mailer = new PHPMailer(true);
$mailer->IsSMTP();
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 465;
$mailer->SMTPAuth = true;
$mailer->Username = 'me#gmail.com';
$mailer->Password = 'password';
$mailer->SetFrom('me#gmail.com', 'Name');
$mailer->AddAddress('you#gmail.com');
$mailer->Subject = 'This is a test';
$mailer->Body = 'Test body';
$mailer->Send();
Hope this helps?
Print your PHPMailer object and check PORT on object and you given PORT
echo "<pre>", print_r($mailer, true);
exit;
Step 1:- Go to https://myaccount.google.com/security#signin then App passwords generate app password.
Step 2:- Paste that 16 digit password $mailer->Password
In most cases you need to create a 2-Step Verification and sign in with an App password.
Small tip: you should carefully read phpmailer's debug message. There could be a proper link to an answer of the problem.
I had this one: https://support.google.com/mail/answer/7126229?visit_id=1-636190350518295662-3485238940&rd=2#cantsignin