Office365 smtp not always working with php mailer - php

I'm trying to use office365 smtp with phpmailer, it authenticates and it delivers sometime but sometimes it does. Not in inbox or spam. It works well with other smtp server. Here is my code:
$mail = new PHPMailer(true);
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
$mail->CharSet = "utf-8";
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = $host;
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
$mail->AuthType = 'LOGIN';
//Provide username and password
$mail->Username = $user;
$mail->Password = $pass;
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "STARTTLS";
//Set TCP port to connect to
$mail->Port = $port;
$mail->From = $from_email;
$mail->FromName = $from_name;
foreach($recipients as $recipients){
$mail->AddBcc($recipients);
}
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->msgHTML($message);

Related

PHPmailer with AWS SES

I am trying to send emails through PHPmailer using the AWS SES SMTP account.
Somehow it sends emails when I ping the file from the terminal but fails to do so when I hit the same file from the browser. Below is the code.
$mail->isSMTP();
$mail->setFrom($sender, 'xx#site.com');
$mail->Username = USERNAME;
$mail->Password = PASSWORD;
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPDebug = 4;
$mail->SMTPSecure = 'tls';
$mail->addAddress('xx#gmail.com');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $bodyHtml;
$mail->AltBody = $bodyText;
$mail->Send();
The error in the browser says: SMTP Error: Could not connect to SMTP host.

Getting SMTP Error "Could not connect to SMTP host" on server but working on my localhost

I am using phpmailer with gmail smtp, and its working for "tls" on my local xampp, but when i moved my project on server mail could not send and gives error: SMTP Error: Could not connect to SMTP host.
My Code is:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Port = 587; // Or 587
$mail->Username = $admin_email;
$mail->Password = $admin_password;
$mail->SMTPSecure = 'ssl';
$mail->From = $admin_email;
// $mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addAddress($emailTo);
if (!$mail->send()){
echo "error";
}
What is wrong in my code?

Error while sending email using php

SMTP -> ERROR: Failed to connect to server: An attempt was made to access a socket in a way forbidden by its access permissions. (10013) The following From address failed: info#gmail.com : Called Mail() without being connected
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "gmail.com";
$mail->Password = "password";
$mail->From = "info#gmail.com";
$mail->FromName = "name.com";
$mail->Subject = "Register";
$mail->MsgHTML($userMsg);
$mail->AddAddress("email address", "name");
//$mail->AddAddress($rowUser['user_email'], $rowUser['user_name']);
$mail->send();
$mail->ClearAllRecipients();
TLS is correct, you could try SSL, change port to 465. Check your credentials. Is on-screen Username "gmail.com"?
Try this:
$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->CharSet = "UTF-8";
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "yourname#gmail.com";
$mail->Password = "yourpassword";
$mail->SetFrom("example#gmail.com");
$mail->Subject = $subject;
$mail->Body =$body;
$mail->AddAddress($email);
if(!$mail->Send())
{
return FALSE;
}

PHPmailer : Showing a weird text before sending a mail?

My problem is clear, PHPmailer is howing a long text before sending a mail, this is my code (function) :
function sendMail($content){
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true;
$mail->Port=465; // Enable SMTP authentication
$mail->Username = 'my mail'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'ssl';
$mail->SMTPDebug=true;
$mail->From = '****';
$mail->FromName = '******';
$mail->addAddress('******');
$mail->Subject = 'subject';
$mail->Body=$content;
$mail->AltBody ='testing';
$stat=$mail->send();
}
and this is a screenshot :
http://i.imgur.com/kLrC97q.jpg
Thanks
Sorry guys, I noticed that I should turn off debugging :
$mail->SMTPDebug=false;

phpmailer & in subject coming in as &

I am sending emails using PHPMailer and through my gmail account with SMTP. It all works except when there is an & in the subject, then in my inbox the subject has &
Any way to make this not happen? I've tried setting the charset and encoding the subject.
My code is below
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = false;
$mail->SMTPAuth = true; // enable 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; // set the SMTP port for the GMAIL server
$mail->Username = "myusername#gmail.com"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->SetFrom('from#somesite.com', 'From Person');
$mail->AddReplyTo("from#somesite.com", "From Person");
$mail->Subject = 'An example with a & in the middle';
$mail->MsgHTML('Some text to send');
$mail->AddAddress('myusername#gmail.com');
$mail->Send()
Thank You
Try using this code, let me know if it helps.
$mail->Subject = "=?UTF-8?B?".base64_encode($_POST['subject'])."?=";

Categories