How to change from email address in smtp php mailer function sendmail() - php

I am using the below code and I got emails but from email address not coming from setFrom() function. Can anyone help me to set desire from email address in SMTP sendmail() function?
include_once 'Mailer.php';
$body = 'TEST, Testing of email.';
$subject = 'Email Demo';
$email = 'test1#gmail.com';
$fromEmail = 'test2#gmail.com';
$objMail = new Mailer();
$objMail->mail->AddReplyTo('test3#gmail.com', 'Test 3');
$objMail->mail->SetFrom('testfrom1#gmail.com', 'Test From 1');
$objMail->mail->AddAddress('test4#gmail.com', 'Test 4');
$objMail->mail->IsHTML(true);
$objMail->sendmail($email, 'Test', $subject, $body);

This has been answered many times before. Gmail does not let you set arbitrary from addresses, though you can add a limited number of preconfigured aliases in your account.

Related

PHPMailer: Can't send email to any kind of domain

I am new to PHPMailer. I am able to send emails to email addresses like abc#gmail.com. But I can't send any email to email addresses like myemail#office.com or myemail#officename.net. PHPMailer does not show any kind of error but it is not sending my email.Here is my phpMailer setup:
require_once('PHPMailer-master/class.phpmailer.php');
$email = new PHPMailer();
$email->From = "info#mydomain.com";
$email->FromName = "Any Name";
$email->AddAddress("enam#domain.com");
$email->AddAddress("enam#domainn.net");
$email->isHTML(true);
$email->Subject = 'Order Confirmation';
$email->Body = "Hello. I am testing <b>PHP Mailer.</b>";
$email->Send();
Can anyone say how can i send email to email addresses of any kind of domain?

add e-mail recipient into php script

CC or forward is not possible to set on mailserver for authorisation mail sent by Joomla itself, but we'd like to store these e-mails.
Question is: how to set it in php of specific plugin? (plugin is sending these e-mails)
code:
// send auth email to user who signed ...
if ($signature_verification = (int)$this->settings->get('security.signature_verification', 0)) {
// unpublished, visitor must verify it first
$this->db->set('published', 0);
$config = JFactory::getConfig();
$from = $config->get('mailfrom', '');
$fromname = $config->get('fromname', '');
$recipient = (string)$this->db->get('email', '');
When I replace last line wth: $recipient = ('my#email.com'), then I get that message, but i want one for visitor and copy for me.
Thanks for advice
OK, actually this piece of code initiates sending of that mail:
if (
$this->sendMail(
$from,
$fromname,
$recipient,
$subject,
$body
) !== true
) {
throw new phpmailerException(JText::_('PLG_CONTENT_CDPETITIONS_EMAIL_SEND_FAILED'), 500);
}
When I make copy of that code, paste it below, and replace $recipient with my e-mail, it works: I have the same message delivered on both adresses. But I need it have it like CC (carbon copy) and have original recipient adress in header of mail, which is delivered to me.
Use the built in mailer methods for Joomla:
$msg = "This is my email message.";
$subject = "Database Update Email";
$to = (string)$this->db->get('email');
$config = JFactory::getConfig();
$fromemail = $config->get('mailfrom');
$fromname = $config->get('fromname');
$from = array($fromemail,$fromname);
$mailer = JFactory::getMailer();
$mailer->setSender($from);
$mailer->addRecipient($to);
$mailer->addRecipient('you1#yourdomain.com');
$mailer->addRecipient('you2#yourdomain.com');
$mailer->addCC('you3#yourdomain.com');
$mailer->addBCC('you4#yourdomain.com');
$mailer->setSubject($subject);
$mailer->setBody($msg);
$mailer->isHTML();
$mailer->send();
This should use PHP to send an HTML email to whomever you want and copy other users on the email through either direct send, CC, or BCC depending on which method you use.

Trying to create a php auto email after a form has been submitted, and style the email with HTML

I'm building a form that allows someone to sign up for a deal. I want the form to send 3 emails using php once it has been submitted; one sending the details of the form to myself, one sending the details to the person offering the deal, and one sending the actual deal confirmation and voucher to the person who signed up.
I've run into problems in that certain email addresses don't seem to receive or be sent the email when i'm testing it (it works for both my hotmail and gmail addresses, but not for my own domain email address). I also can't figure out how to add some sort of html style to the emails being sent.
How can I style these emails and make sure that they are being sent to right email addresses?
here is the code:
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$airline=$_REQUEST['airline'];
$position=$_REQUEST['position'];
$checkin=$_REQUEST['checkin'];
$attendance=$_REQUEST['attendance'];
$email=$_REQUEST['email'];
$terms=$_REQUEST['terms'];
if (($name=="")||($position=="")||($checkin=="")||($attendance=="")||($email==""))
{
echo "All fields are required, please fill the form again.";
}
//email going to me//
else{
$from="From: Deal 1 Form Submission<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemail#test.com",
$subject,
$message="someones taken the deal: heres their info: <br> Name: $name\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n" );
echo "Email sent!";
}
//email going to the customer//
{
$from="From: me<myemail#test.com>\r\nReturn-path: $email";
$subject="Thank you for choosing this deal";
mail("$email", $subject, $message="thanks for choosing this deal!, please present this voucher when attending the restaurant", $from );
}
//email going to the partner//
{
$from="From: Me<myemail#test.com>\r\nReturn-path: $email";
$subject=" Great news! Someone has chosen your deal";
mail("partneremail#test.com",
$subject,
$message="Fantastic news, a customer has taken your deal! here's their info: Name: $name\nAirline: $airline\nPosition: $position\nCheckin: $checkin\nAttendance: $attendance\nEmail: $email\n
", $from );
}
}
?>
Many thanks.
Your best bet is to use PHPMailer. Here is some example code that relays the email through gmail. The reason why you would want to do this is to avoid emails from the server going into you spam box.
$debug = true; // set it to false in production
$Mail = new PHPMailer();
if ($debug) {
// this allows you to see the details of the connection to gmail
$Mail->SMTPDebug = 4;
}
$Mail->isSMTP();
$Mail->Host = 'smtp.gmail.com';
$Mail->SMTPAuth = true;
$Mail->Username = 'your#gmail.com';
$Mail->Password = '[your-password]';
$Mail->SMTPSecure = 'tls';
$Mail->Port = 587;
$Mail->setFrom('from#address.com', 'John Smith');
$Mail->addAddress('to#address.com', 'Someone Else');
$Mail->addReplyTo('from#address.com', 'John Smith');
$Mail->addBCC('bcc#address.com', 'BCC Person');
$Mail->addAttachment('path/to/img/or/pdf/or/whatever/you/want/to/attach.pdf');
$Mail->Subject = 'Test Subject';
$Mail->Body = '<h1>Hi! This is an HTML format body'
$Mail->AltBody = 'Hi! Im just a text email in case HTML is not supported!';
if (!$Mail->send()) {
print $Mail->ErrorInfo;
return false;
} else {
return true;
}

PHP error when trying to send email using Swift Mailer

I ve tried everything, i don't know how to fix this, so, i am using this Swift Mailer library to send a confirmation email. So here is the code from my index.php
if($confirm){
//include the swift class
include_once 'inc/php/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){
//email sent
$action['result'] = 'success';
array_push($text,'Thanks for signing up. Please check your email for confirmation!');
}else{
$action['result'] = 'error';
array_push($text,'Could not send confirm email');
}
}
And my send_email function is in another php file functions.php
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl)
->setUsername('my email here')
->setPassword('my password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('somedomain.com' => 'somethinghere'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
The error that i am getting is
Fatal error: Call to undefined method Swift_MailTransport::setUsername() in /srv/disk7/something/www/something/signup/inc/php/functions.php on line 31
How can i fix this? I am a beginner in php.
I'm guessing Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl) fails to create the expected instance.
BTW, shouldn't it be Swift_MailTransport::newInstance('smtp.gmail.com',465,'ssl') (i.e. smtp.gmail.com and ssl in quotes)?
Swift_MailTransport, from the documentation...
...sends messages by delegating to PHP's internal mail() function.
In my experience -- and others' -- the mail() function is not particularly predictable, or helpful.
Another thing it does not have is a setUsername or setPassword method.
I'd say you want to use Swift_SmtpTransport
One more thing; as pointed out by others, your string arguments should be quoted, ie
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
Looks like you haven't included the Swift Mail class.

How do I show a different from address in my emails? using zend framework

FIrst of all I don't intend to spam - I 'm building email facility in my application and would like to be able to include any email address in the from header of the email while be able to send it from any mail server i.e like show a yahoo address in the email from field while send it from my own smtp server? Is it possible? I'm using php and zend framework here
Zend_Mail allows you to specify all that.
Setting up transport (during bootstrap):
$tr = new Zend_Mail_Transport_Smtp($mail_smtp_host, array(
'auth' => 'login',
'username' => $mail_smtp_username,
'password' => $mail_smtp_password,
'port' => $mail_smtp_host_port,
));
Zend_Mail::setDefaultTransport($tr);
Sending a message anywhere in your app:
$mail = new Zend_Mail();
$mail->setFrom($email_from, $email_from_name)
->addTo($email_to)
->addCc($email_cc)
->addBcc($email_bcc)
->setSubject($email_subject)
->setBodyHtml($email_html)
->setBodyText($email_text)
->send()
;
The following works with sendmail and should work with sendmail-like smtp servers. If the from domain does not match the domain of the originating server, the chance of being flagged as spam increases.
$msg = 'my message body';
$subject = 'my message';
$to = 'email#example.com';
$from = "ali#yahoo.com";
$headers .= "From: $from\r\n";
$flags = '-f "$from"';
mail($to, $subject, $msg, $headers, $flags);
You can send through Yahoo's SMTP server.
smtp.mail.yahoo.com, SSL, port 465, username/password for the Yahoo address.

Categories