PHPMailer does not send mail to group email - php

I am able to send emails to individual email ids using PHPMailer.
But, I am not able to send email to group email using PHPMailer. I am not talking about sending multiple emails using loop.
Suppose, I have an email group#example.com and this is a group email. That means, if we send email to this, it will be received by all the people added to this email group.
I tried sending email to group email but no one received the email. I am using same domain sender email id. For ex: noreply#example.com
My code looks like below
$mail = new PHPMailer();
$mail->From = 'noreply#example.com';
$mail->FromName = 'FromName';
$mail->Subject = 'Subject';
$mail->Body = 'Body';
$mail->isHTML(true);
$mail->AddAttachment($file);
$mail->AddAddress($groupEmail);
if(!$mail->send()){
//error msg
}

Related

Mail using custom function

So as stated in the title I wanna created a function different from mail to send mail(confusing ik)
So using mail () it sends the email from my server regardless of the headers I put so I need to create a function to send the actual email from a set email
Let's say I have an email
flameforgedinc#gmail.com
Lets say I have a bunch of emails in a mailing list
Now I have some code that's gonna use this function to email each one
So this code use's cMail ($to, $sub, $msg, $from);
And the email will appear to the user
From: flameforgedinc#gmail.com
And I actually want it to come from my email
If I use mail then it comes from my server and displays altervista00000 and I don't want the plus my server limits the mail() function to activation emails and I need to be able to send newsletters
Any ideas or workarounds??
My name is Pavel Tashev. I will try to help ;)
The easiest and more correct way from technical point of view is to use your own Mail server which hosts your email account and sends your emails. The purpose of PHP in that cases will be to tell the mail server to send email X to a list of emails Y, from an email account Z.
This will solve all your problems:
max allowed emails per hour;
sender name;
The good news is that you already have a Gmail account which is hosted by Google and you don't need to build your own mail server. You can use Google's mail server. Also, for the email sending I would advice you to use PHPMailer (url: https://github.com/PHPMailer/PHPMailer).
Here is how we can do all of this. Follow these steps:
Integrate PHPMailer in your project. If you use composer, that will be a straightforward process. If you don't, simply download the code and include this file PHPMailerAutoload.php in your project. Just follow the instructions on Github. It is really easy.
So, when you are ready with the installation of PHPMailer you must tell it to connect to your mail server. For Gmail and your email account this would look like this:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'flameforgedinc#gmail.com';
$mail->Password = 'PUT-YOUR-PASSWORD-HERE';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('flameforgedinc#gmail.com', 'YOU CAN PUT YOUR NAMES HERE IF YOU WANT');
The final step is to set up the recipient and the content of the email:
$mail->addAddress('some.user#example.net', 'Joe User');
$mail->addAddress('seconrd.user#example.com', 'The name is optional');
$mail->addReplyTo('flameforgedinc#gmail.com', 'YOU CAN PUT YOUR NAMES HERE IF YOU WANT');
$mail->isHTML(true);
$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';
now you will have to send the email:
!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I hope that will help.

SetFrom PHPMailer not working

I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "myusername#gmail.com";
$mail->Password = "gmail_password";
$mail->From = 'donotreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->AddAddress('Toreceiver#test.com', 'Receiver'); // Add a recipient
$mail->IsHTML(true);
$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;
$mail->Body = "This is in <b>Blod Text</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
It is sending mail from myusername#gmail.com but I want it to send with 'donotreply#mydomain.com' as set in $mail->From. Any help will be highly appreciated.
Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.
My solution was to add
$mail->AddReplyTo($FromEmail, $FromName);
This way at least if you reply to the message it will be delivered as described
There is another way to set from address in phpmailer and it is better supported. I found that server where i used phpmailer didn't pass email to another server from the same host. But then i change the way how i set the from address and it solve the problem.
Use:
$mail->SetFrom('donotreply#mydomain.com', 'Admin');
Instead of $mail->From and $mail->FromName.
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
settings > Accounts and import > Send mail as:
set another email which you want to use as from:
if you are using zoho then you can follow:
settings > Mail tab > Send mail as > add from address
then verify that email.
For GSuite users...
What Jyohul said, is the correct way of doing it. You can add an alias in Google Admin Console, under "Users". Click on the user's name, and then click on "user information". In there you can add Aliases. Once the aliases are added, you can then do what Jyohul said, which I added a needed step...:
if you want to use different email address as sentFrom, then you can set your email from gmail settings:
Go to your Gmail, then click the gear on the top right, then:
settings > Accounts > Send mail as: then click "Add another email address".
Upgrade to the latest version of PHPMailler. You should also make sure that you turn on debuging in oder to view erro messages.
$mail->SMTPDebug = 2;
You will the identify the error. Also make sure your SMTP Server credentials are correct. Like host, username and password.
Mine worked correctly

Putting E-mail into "sent" folder after PHP mail function

I am currently working on a project where I am accessing an e-mail account using PHP's imap_open(). I know that I can send an e-mail with PHP using the mail() function.
However, I was wondering if, after I send an e-mail, I could place that e-mail in the email account's sent folder using any of PHP's imap functions.
Any help would be greatly appreciated.
This is more to email account setting to save the email sent to sent account folder.
You may try with external mail that already configured to save email sent to sent folder e.g Gmail http://www.vulgarisoip.com/files/phpgmailer.zip
example :
require_once('/phpgmailer/class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'user#domain.com';
$mail->Password = 'password';
$mail->From = 'user#domain.com';
$mail->FromName = 'User Name';
$mail->Subject = 'Subject';
$mail->AddAddress('myfriend#domain.com');
$mail->Body = 'Hey buddy, here\'s an email!';
$mail->Send();

Sending bulk mail using phpmailer

I am new to Phpmailer and I am using it to send a bulk Email to over a thousand people from a noreply account. The code works fine when I send the Email to one or two people but when I send it to everybody (including myself) it goes to spam. One more problem is in details of the Email it shows the Email ids of all the people to whom it was sent which I don't want it to do.
The code is as follows:
//date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp1.site.com;smtp2.site.com";
$mail->SMTPAuth = true;// enable SMTP authentication
$mail->SMTPKeepAlive = true;// SMTP connection will not close after each email sent
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('noreply#mydomain.com', 'List manager');
$mail->AddReplyTo('list#mydomain.com', 'List manager');
$mail->Subject = 'Newsletter';
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress($row[0]);
$mail->Send();//Sends the email
}
As JoLoCo points out, the AddAddress() method just ADDS a new address to the existing recipient list. And since you're doing it as an add/send loop, you're sending out a helluva lot of duplicate copies to the first recipient, one less to the second, etc...
What you need is:
while($row = mysql_fetch_row(...)) {
$mail->AddAddress($row[0]);
$mail->send();
$mail->ClearAllRecipients(); // reset the `To:` list to empty
}
On the other hand, since this spams your mail server with a lot of single emails, another option is to generate one SINGLE email, and BCC all the recipients.
$mail->AddAddress('you#example.com'); // send the mail to yourself
while($row = mysql_fetch_row(...)) {
$mail->AddBCC($row[0]);
}
$mail->send();
This option is most likely preferable. You only generate a single email, and let the mail server handle the heavy duty work of sending out copies to each recipient.
I think you're adding the new address to the email already sent -- so the first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on.
Also, I don't think you need to set the AltBody and MsgHTML every time.
You should add all the addresses to the BCC field first, then send.
So try...
// rest of code first
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("you#example.com")
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AddBCC($row[0]);
}
$mail->Send();//Sends the email
Use BCC (Blind Carbon Copy) to hide the list of recipients.
Related to the spam problem, it depends on the email provider of the recipients what is going to spam, and what is not, and there are many factors out there.

Send email from PHP and catch the autoresponses

I'm sending emails using a PHP script and class.phpmailer.php.
I need to be able to 'catch' bounces and auto-responses. I'm able to catch the bounces already. I created an alias that redirects bounces to a php script, and I parse the email there. I include some information in the original email in the headers, so I can know which emails bounced.
The same logic should work for the auto-responses, I think the problem is that the email are not getting to the server. I already have a reverse DNS configured, pointing to the server IP address.
This is part of how I send an email:
$mail = new PHPMailer();
$mail->From = $fromAddr;
$mail->Sender = $sender;
$mail->FromName = $fromName;
$mail->AddAddress($email);
$mail->IsHTML(true);
$mail->Subject = $subject;
the I add the headers, for example:
$mail->AddCustomHeader($mail->HeaderLine("From", $fromAddr));
$mail->AddCustomHeader($mail->HeaderLine("Subject", $subject));
$mail->AddCustomHeader($mail->HeaderLine("Company-State", "Florida"));
$mail->AddCustomHeader($mail->HeaderLine("Company-Country", "USA"));
$mail->AddCustomHeader($mail->HeaderLine("From", $fromAddr));
$mail->AddCustomHeader($mail->HeaderLine("Reply-To", $fromAddr));
My RDNS is something like: mail2.mydomain.com. The bounces are going to "www-data".
What should I add and where? something like www-data#mail2.mydomain.com?
Tks!

Categories