Pear PHP Mailer Emails Sender and Recipient - php

I'm using Pear PHP Mailer to send an email through SMTP to a number of recipients, but for each one, it sends an additional email to the sender's email address as well.
This is my code: Any help is appreciated.
...
$from = "$from_email_name <$from_email_addr>";
$html_body = "$html";
$crlf = "\r\n";
$hdrs = array(
'From' => $from,
'Subject' => $subject
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text_body);
$mime->setHTMLBody($html_body);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail = Mail::factory('mail',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to_email_addr, $hdrs, $body);
....
Also, on the email it is sending to my sender address, its saying the correct recipient name and sender information, but its like the sender is getting a copy of each one it sends out.

It could be a "service" of your mail server provider to automatically put mails sent via the SMTP server in your IMAP/POP mail box. Ask him if that's the case.

Related

Pear PHP Mail sending CC doesn't seem to work

I'm using PEAR's mail library via SMTP on my server and whilst I can get emails to generate, adding CC's doesn't seem to work. Basically the CC recipients never get their email even though the main recipient of the same email does.
My basic setup is as follows, all the recipient variables ($to,$cc,$bcc) are string variables containing either a single recipient email addresses or comma separated email addresses.
$headers = array (
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject,
'Reply-To' => $from,
'X-Mailer' => 'PHP/' . phpversion(),
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=ISO-8859-1'
);
$smtp = Mail::factory('smtp', array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
));
$result = $smtp->send($to, $headers, $message);
I've read that sending BCCs is more complex, so lets stick to the CCs... why aren't they being received ? Is there anything obvious that I'm doing wrong here ?
In order to send an email to CC or BCC with SMTP, you must list all email addresses both under recipients to the send() function and in the CC key within the header.
$to = "john#example.com";
$cc = "doe#example.com";
$recipients = $to . ", " . $cc;
$headers["From"] = "john#example.com";
$headers["To"] = $to;
$headers["Subject"] = "Hello World!";
$headers["Cc"] = "doe#example.com";
$headers["Reply-To"] = "john#example.com";
$send = $mail->send($recipients, $headers, $body);

How to send logo in the body of email in php?

I am attaching my email code. I want to add an image in my body section with a message in this code.
require_once "../lib/Mail-1.4.1/Mail.php";
$from = 'someone#gmail.com';
$to = $mail;
$subject = 'Account Confirmation';
$body = 'Thanks for registering yourself, your login credentials are as
follows;'."\r\n".'Your User Name is: '.$uname.''."\r\n".'Your Password is:
'.$pass1.'';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'someone#gmail.com',
'password' => '12345678'
));
$mail = $smtp -> send($to, $headers, $body);
if(PEAR::isError($mail)){
echo '<p>'.$mail->getMessage().'</p>';
}
else{
echo "<script>alert('Successfully Registered! Your credentials have been
sent on email that you have given in the form')</script>";
}
this is my email code, in body I want to add an image/logo.
Use for embedding image AddEmbeddedImage
you can also add an image as inline attachment with the content ID of my-photo, you would access it within the HTML body using <img src="cid:my-photo" alt="my-photo">.
In detail, here is the function to add an embedded attachment:
$mail->addEmbeddedImage($filename, $cid);

PEAR: Mail Mime - Can't send email after uploading to hosting site - GoDaddy

I've been creating this project, where the system accepts registration and then send the information thru mail to the owner. I'm using PEAR: Mail Mime to do this. Sending of mail is actually working on my local computer, but after uploading this project to a hosting site (GoDaddy), it's not sending anymore.
I did an error check to see what hinders the sending of mail, and I received this message:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm using a GMail account to send email messages, and use this code to do it:
/* INCLUDE PREREQUISITES IN ORDER TO SEND AN EMAIL */
include('../../application/third_party/Mail-1.3.0/Mail-1.3.0/Mail.php');
require_once '../../application/third_party/Mail_Mime-1.10.0/Mail_Mime-1.10.0/Mail/mime.php';
/* HEADER INFORMATION */
$from = '<***#gmail.com>';
$to = '<***#gmail.com>';
$subject = 'Someone registers';
$crlf = "\r\n";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
// CREATING THE MIME MESSAGE
$mime = new Mail_mime($crlf);
$html = '
<html>
<body>
Message here
</body>
</html>';
// SETTING THE BODY OF THE EMAIL TO HTML
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
// SENDER INFORMATION
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$errormessage = '<p>'.$mail->getMessage().'</p>'; // ERROR MESSAGE
$mail->getUserInfo();
}
Yes, I turned on the Access for less secure apps of the GMail sender account.
And yes, the error displaying is turned on.
ini_set('display_errors', 1);
error_reporting(E_ALL);
Any solutions that I might have missed?
Found an answer regarding the outgoing mail in this - Sending email through gmail SMTP on GoDaddy.
It is because of the hosting provider, GoDaddy, which restricts the SSL port of GMail (465), and other ports such as 25 and 587. Thanks to this article -Troubleshooting PHPMailer Problems - of GitHub for clearing that up. Users are resorted to use the mail-server of GoDaddy instead.
But I still managed to send an email using GMail by doing:
$smtp = Mail::factory('smtp');
/* SETTINGS WILL BE IN DEFAULT (E.G. HOST:LOCALHOST, PORT: 45) */
instead of
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '***#gmail.com',
'password' => '***'
));
But the receiver receives the sent email as "spam" - result will be that the receiver won't notice the incoming emails from the system (not a good choice). And there are no records in the Sent items of the sender (means you "used" the email to send an email, but not entirely).
So, we are back with the mail-server of GoDaddy. First, you have to determine the relay server of your hosting account by checking this What is the name of my hosting account's relay server?, which was published by GoDaddy themselves. In my case, Linux (cPanel), which I'll be using localhost as my host.
$smtp = Mail::factory('smtp', array(
'host' => 'localhost',
'auth' => true,
'username' => '***#***.com',
'password' => '***'
));
But the weird thing is, there are still no records of sent items. Hope a GoDaddy representative reads this.

Cannot send mail using PEAR Mail for port 587 but yes for port 25

I cannot seem to get port 587 to send mail. I am capable of sending mail using port 25. I'm using PEAR Mail to send this.
require_once "Mail.php";
/**************************************************
EDIT the following variables for your own use
***************************************************/
$from = "Noreply <noreply#host.com>";
$to = "Abc <abc#host.com>";
$subject = "Hi!"; //type in subject here
$host = "mail.host.com"; // also tried ssl://mail.host.com and tls://mail.host.com
$username = "noreply#host.com"; // Your user / full email address
$password = "password"; // Password to your email address
/**************************************************
***************************************************/
$body = "test message";
$headers = array (
'From' => $from,
'To' => $to,
'Date' => date('Y-m-d H:i:s'),
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port'=>587));
$mail = $smtp->send($to, $headers, $body);
Enable SMTP TLS / SSL in PEAR::MAIL
Edit the file - /pear/Net_SMTP/Net/SMTP.php
Find the line with the following content (approximately line 590):
function auth($uid, $pwd, $method = '', $tls = false, $authz = '')
Replace this with:
function auth($uid, $pwd, $method = '', $tls = true, $authz = '')
So your changing '$tls' from 'false' to 'true'
Save the file
And try with port 587

Pear Mail stripping part of link

I have a bit of a strange one. Using PHP Pear Mail I am sending an HTML email with a link in to a subdomain http://mysub.mydomain.co.uk
$body='<html><body><strong>Hello '.$forename.'</strong><br><br>Thank you for registering your details. To complete the process, please follow the link below in this email.<br><br>
Complete Verification Here</body></html>';
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$mime = new Mail_mime();
$mime->setHTMLBody($body);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password)
);
$mail = $smtp->send($to, $headers, $body);
The email gets sent fine and if I print the body of the email onto the screen from the page sending the email the link works fine. However when it arrives in an email in MS outlook it is taking the first 2 characters out of myvalue in the link. If myvalue=12345678 it says myvalue=345678 and displays the link incorrectly for example it shows the above as ttp://mysub.mydomain.co.uk/?V=345678. Notice it removes the H in the http address and also adds a forward slash before the ?v= and the first 2 digits 12 are also missing. It then fails to open the link as it is displaying it incorrectly as a http link. The email also does not arrive as an HTML email at Gmail and there is no link.
Any idea what I am missing here?
Eventually found the issue
In the body text I had to replace every occurrence in the body of
"
with ASCII code
&#039
Now working fine
You don't have to manipulate the body text outside of the mail_mime package, you need to mime encode the body with it:
$mime = new Mail_mime();
$mime->setHTMLBody($body);
$mimebody = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
'smtp',
[
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => $port
]
);
// send email
$mail = $smtp->send($to, $headers, $mimebody);

Categories