PHPmailer double from inside header - php

When I send a email by a PHP script the receivers sees two "from" addresses, like this
Subject: About this
From: me#example.com
From: me#web01.example.com
Receiver: you#example.com
How can I change or get the second from with the webserver out of there?
This problem occurs since the update of Apache and MySQL...
Before there wasn't a second from:
Any suggestions?
Here a little piece of PHP code what has been used.
// Class start
$mail = new PHPMailer();
$mail->IsHTML(true);
// From
$mail->From = $config['email'];
$mail->FromName = $config['name'];
// To
if(!is_array($email)){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
} else{
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
If I take a look at email from before the update the Return Path + Sender (inside Header) are the same as the email adres. Like it should be.
But after the update the Return Path + Sender say me#web01.example.com so that looks like the problem?
I am now using SMTP and sendmail was giving these problems since the update. SMTP solved it.

Related

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

Sending a mail with php: Script is waiting for the answer?

I am using the class PHPMailer to send Mails via SMTP:
<?php
require 'php_mailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.dfgdfgdfg.de'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'dfgdfg'; // SMTP username
$mail->Password = 'dfgsdfgdsfg'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'community#fdgdfg.de';
$mail->FromName = 'dfgdfgdg';
$mail->AddAddress('interview#dfgdfg.de', 'Udo'); // Add a recipient
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'HTML-Mail mit Logo';
$mail->Body = 'Nachfolgend das <b>Logo</b>';
$mail->AltBody = 'Aktiviere HTML, damit das Logo angezeigt wird';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
My Questions:
Whats the best way to send to a lot of Mails (same Mailtext, only the appellation is different (Hello $NAME)?
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Thanks!
Alex
You are setting PHPMailer to interact with SMTP, so I guess that it will wait for it to complete. This is not optimal, because as you say you will block the PHP script until SMTP responds.
It would be better to send through your localhost: set PHPMailer to use sendmail, which will usually be a wrapper to a local exim4 or postfix, which will then handle the mailing for you. This is much better, also because the local server will handle any possible temporary error, and retry later. PHP won't.
You may also want to explore other options, like Mandrill or Sendgrid to do the job, especially if you do lot of mailing or bulk mailing.
Whats the best way to send to a lot of Mails (same Mailtext, only the
appellation is different (Hello $NAME)?
You can do something like, Set the name too.
// rest of code first
$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
You can have special string 'name_here' in the body and place $name with str_replace function
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Yes according to my knowledge you will have to wait.
How to do a str_replace ? Assume that your email body is as follows
$body = " Dear %first_name%,
other stuff goes here....... ";
$body = str_replace("%first_name%", $first_name, $body);
above will replace %first_name% with the name($first_name) you provide.

Sending emails with PHP fails

Situation:
Our client (owner of domain.com) has set the A record for www.domain.com to the IP address of one of our servers where we run the website behind domain.com. We only provide hosting for this domain, they have their own email servers.
This means that domain.com has another IP than the mail server for domain.com.
Problem:
Sending mails from PHP to foo#bar.com works BUT sending mails to *#domain.com does not work.
Question:
Does this has something to do with SPF records?
How do I solve this?
thx
Bundy
This happened to me on a shared host as well as is probably because there is a local delivery mechanism in place on the web server, i.e. when your web server sees an email for #domain.com it assumes it will be the one to handle, and does not pass in on to the actual mail server.
Go into your web server's panel (Cpanel or whatever) and check your email settings for this domain. Make sure "local delivery" or something similar is disabled for domain.com
Does your main domain.com (without www.) have an CNAME record? This will automatically refer the mx record of the domain to the cname record.
The characteristic payload information of an MX record is the fully
qualified domain name of a mail host and a preference value. The host
name must map directly to one or more address record (A, or AAAA) in
the DNS, and must not point to any CNAME records.
http://en.wikipedia.org/wiki/MX_record#cite_note-0
The best way to send emails is using a smtp method:
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/
Example file:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "mail.example.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "user#example.net"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "example#example.net";
$mail->FromName = "Mailer";
$mail->AddAddress("destiny#example.net");
//$mail->AddReplyTo("info#example.com", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$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";
?>

send email from localhost to external email account

if(mysql_affected_rows()==1)
{
$msg="To activate your account, please click on the following link:\n\n
http://localhost:80/activate.php?email=".urlencode($email)."&key=".$activation;
if(mail($email,"Registration Confirmation", $msg, "From: myemail#mycompany.com\r\nX-
Mailer: php"))
{
echo '<div> Thank you for registering. A confirmation email has been sent to '.
$email.'.Please click on the link to activate your account then</div>';
}
}
I use that code snippet to send an email from myemail#mycomapany.com to $email (myyahoo#yahoo.com) but I fail to receive any email in the yahoo account. I have got the echoed message displayed in the browser to indicate the successful mailing, though. Also, I have tried sending an email from myemail#mycomapany.com to the yahoo account directly via Outlook and it works fine. Would someone please help me fix that source snip or any kind of extra settings to make the mail function work ? Thank you so much.
EDIT: By the way, I also set up my php.ini's stmp script block as follows,
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = mail.mycompany.com
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = postmaster#localhost
My problem is to set my local computer as a local mail server, and i editted php.ini as above but it still doesn't work.
[RESOLVED]
<?php
function SendMail($from, $to, $subject, $body)
{
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.mycompany.com";
$mail->SMTPAuth = true;
$mail->Username = "My name"; // SMTP username
$mail->Password = "mypassword"; // SMTP password = one used in Outlook
$mail->From = "myemail#mycompany.com";
$mail->FromName = "Registration Confirmation Email";
$mail->AddAddress($to);
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
return false;
}
return true;
}
?>
I include the above code snip to demonstrate a small but working example that uses phpmailer. Thanks to the one who introduces it to me in the post below.
Take a look at phpMailer it's good and very easy to use, and you can send email-s from your existing email accounts (like gmail)
Try this one, from this script you can also send email from your local file to external email.
require_once('class.phpmailer.php');
define('SMTPSERVER', 'mail.yourcompany.com');// sec. smtp server
define('SMTPUSER', 'info#yourcompanyname.com'); // sec. smtp username
define('SMTPPWD', '123456'); // sec. password
$useremail = 'mail#mail.com';
$msg = 'your text here';
$from = 'info#yourcompanyname.com';
$mailTest=new EmailService();
if ($mailTest->generalMailer($useremail, $from, 'Yoursite.com', 'Your company name', $msg)) {
} else {
if (!$mailTest->generalMailer($useremail, $from, 'Yoursite.com', 'Your company name', $msg)) {
if (!empty($error)) echo $error;
} else {
echo 'Yep, the message is send (after hard working)';
}
}
header("location:index.php?email_msg=Email sent successfully");
You should look into mail headers. Most of the time the reason is either:
Skipped Reply to/From headers
Skipped X-Mailer
For further details, please see here.
Edit
Ok, I think I know where the problem might be.
There is quite a big difference between LAMP and WAMP. Linux systems tend to have the sendmail library, which allows you to send mail to other systems without configuring anything. Windows does not.
Therefore, you should have an SMTP server installed, in order to send mail to outer sources. Take a look at this question for details.
Also, if you have a need to test this, you should take a look at Papercut, which is indispensable while testing your system.
If you can send a plain mail(), you should be able to send a mail from a library too. Not the opposite way around. Only exception is, if you use an already existing SMTP/IMAP server to do this, e.g. Gmail.
Are you sure that you have an MTA (Mail Transport Agent = Mailserver) installed on your local machine? Otherwise, you'll need to use your ISP's SMTP server instead.
Mostly that will be mail.yourprovider.com or smtp.yourprovider.com (the same hostname as what you use for outgoing mail in Outlook). PHP cannot mail by itself, it needs an MTA (either locally or remote) to do so.

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