Returns false if the operation failed.
public smtpConnect ( array $options = null ) : boolean | ||
$options | array | An array of options compatible with stream_context_create() |
return | boolean |
function enviarMail($receptor, $asunto, $mensaje) { $mail = new PHPMailer(); $mail->IsSMTP(); $mail->CharSet = 'UTF-8'; $mail->SMTPAuth = 'true'; $mail->SMTPSecure = 'ssl'; $mail->SMTPKeepAlive = true; $mail->Host = 'smtp.gmail.com'; $mail->Port = 465; $mail->IsHTML(true); $mail->Username = "******"; $mail->Password = "******"; $mail->SingleTo = true; $from = '*****@*****.**'; $fromname = 'CoWorking'; $subject = $asunto; $message = $mensaje; $headers = "From: {$from}\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=UTF-8\n"; $mail->From = $from; $mail->FromName = $fromname; $mail->AddAddress($receptor); $mail->Subject = $subject; $mail->Body = $message; $options = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)); $mail->smtpConnect($options); if (!$mail->Send()) { return false; } else { return true; } }
/** * Test SMTP host connections. * This test can take a long time, so run it last. */ public function testSmtpConnect() { $this->assertTrue($this->Mail->smtpConnect(), 'SMTP single connect failed'); $this->Mail->smtpClose(); $this->Mail->Host = 'ssl://localhost:12345;tls://localhost:587;10.10.10.10:54321;localhost:12345;10.10.10.10'; $this->assertFalse($this->Mail->smtpConnect(), 'SMTP bad multi-connect succeeded'); $this->Mail->smtpClose(); $this->Mail->Host = 'localhost:12345;10.10.10.10:54321;' . $_REQUEST['mail_host']; $this->assertTrue($this->Mail->smtpConnect(), 'SMTP multi-connect failed'); $this->Mail->smtpClose(); $this->Mail->Host = ' localhost:12345 ; ' . $_REQUEST['mail_host'] . ' '; $this->assertTrue($this->Mail->smtpConnect(), 'SMTP hosts with stray spaces failed'); $this->Mail->smtpClose(); $this->Mail->Host = $_REQUEST['mail_host']; //Need to pick a harmless option so as not cause problems of its own! socket:bind doesn't work with Travis-CI $this->assertTrue($this->Mail->smtpConnect(['ssl' => ['verify_depth' => 10]]), 'SMTP connect with options failed'); }
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = EMAIL_USER; // SMTP username $mail->Password = EMAIL_PASS; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to $options = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); $mail->smtpConnect($options); $mail->From = '*****@*****.**'; $mail->FromName = 'Daniel Paschal'; $mail->addAddress('*****@*****.**', 'Daniel'); // Add a recipient //$mail->addAddress('*****@*****.**'); // Name is optional $mail->addReplyTo('*****@*****.**', 'Dan'); //$mail->addCC('*****@*****.**'); //$mail->addBCC('*****@*****.**'); //$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';
<?php date_default_timezone_set('Asia/Calcutta'); require 'PHPMailerAutoload.php'; $to = "*****@*****.**"; $subject = "test"; $message = "test"; //Create a new PHPMailer instance $mail = new PHPMailer(); if ($mail->smtpConnect()) { //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = 'mail.gmail.com'; //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 587; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'tls'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = "******"; //Password to use for SMTP authentication $mail->Password = "******";
<?php require __DIR__ . '/../vendor/autoload.php'; //start setup mail config $mail = new PHPMailer(); $mail->isSMTP(); $mail->Host = 'smtp'; $mail->Port = 25; //end setup mail config //start setup mail $mail->setFrom('*****@*****.**', 'From user'); $mail->addAddress('*****@*****.**', 'To User'); $mail->Subject = 'Test mail subject line'; $mail->Body = 'This is <b>test</b> message: ' . time(); //end setup mail //start send actual email $mail->smtpConnect(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true]]); if (!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } //end send actual email
function checkSMTP($hostname, $username, $password, $port, $ssl) { require 'phpmailer/PHPMailerAutoload.php'; // Required PHPMailer $mail = new PHPMailer(); $mail->isSMTP(); $mail->SMTPDebug = 0; $mail->Host = $hostname; $mail->SMTPAuth = true; $mail->Username = $username; $mail->Password = $password; // Check if SSL is enabled if ($ssl == "true") { $mail->SMTPSecure = 'ssl'; } else { $mail->SMTPSecure = 'tls'; } $mail->Port = $port; // Check if SMTP connection can be made, if so return true, else return false if (!$mail->smtpConnect()) { return false; } else { return true; } }
private function enviar_email($doc) { if ($this->empresa->can_send_mail()) { if ($_POST['email'] != $this->proveedor->email) { $this->proveedor->email = $_POST['email']; $this->proveedor->save(); } /// obtenemos la configuración extra del email $mailop = array('mail_host' => 'smtp.gmail.com', 'mail_port' => '465', 'mail_user' => '', 'mail_enc' => 'ssl', 'mail_low_security' => FALSE); $fsvar = new fs_var(); $mailop = $fsvar->array_get($mailop, FALSE); $filename = 'albaran_' . $this->albaran->codigo . '.pdf'; $this->generar_pdf_albaran($filename); if (file_exists('tmp/' . FS_TMP_NAME . 'enviar/' . $filename)) { $mail = new PHPMailer(); $mail->IsSMTP(); $mail->SMTPAuth = TRUE; $mail->SMTPSecure = $mailop['mail_enc']; $mail->Host = $mailop['mail_host']; $mail->Port = intval($mailop['mail_port']); $mail->Username = $this->empresa->email; if ($mailop['mail_user'] != '') { $mail->Username = $mailop['mail_user']; } $mail->Password = $this->empresa->email_password; $mail->From = $this->empresa->email; $mail->FromName = $this->user->nick; $mail->CharSet = 'UTF-8'; $mail->Subject = $this->empresa->nombre . ': Mi ' . FS_ALBARAN . ' ' . $this->albaran->codigo; $mail->AltBody = 'Buenos días, le adjunto mi ' . FS_ALBARAN . ' ' . $this->albaran->codigo . ".\n" . $this->empresa->email_firma; $mail->WordWrap = 50; $mail->MsgHTML(nl2br($_POST['mensaje'])); $mail->AddAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $filename); $mail->AddAddress($_POST['email'], $this->proveedor->razonsocial); $mail->IsHTML(TRUE); $SMTPOptions = array(); if ($mailop['mail_low_security']) { $SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)); } if ($mail->smtpConnect($SMTPOptions)) { if ($mail->Send()) { $this->new_message('Mensaje enviado correctamente.'); } else { $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); } } else { $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); } unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $filename); } else { $this->new_error_msg('Imposible generar el PDF.'); } } }
private function enviar_email($doc, $tipo = 'simple') { if ($this->empresa->can_send_mail()) { if ($_POST['email'] != $this->cliente->email and isset($_POST['guardar'])) { $this->cliente->email = $_POST['email']; $this->cliente->save(); } if ($doc == 'factura') { $filename = 'factura_' . $this->factura->codigo . '.pdf'; $this->generar_pdf_factura($tipo, $filename); } else { $filename = 'albaran_' . $this->albaran->codigo . '.pdf'; $this->generar_pdf_albaran($filename); } if (file_exists('tmp/' . FS_TMP_NAME . 'enviar/' . $filename)) { $mail = new PHPMailer(); $mail->CharSet = 'UTF-8'; $mail->WordWrap = 50; $mail->isSMTP(); $mail->SMTPAuth = TRUE; $mail->SMTPSecure = $this->empresa->email_config['mail_enc']; $mail->Host = $this->empresa->email_config['mail_host']; $mail->Port = intval($this->empresa->email_config['mail_port']); $mail->Username = $this->empresa->email; if ($this->empresa->email_config['mail_user'] != '') { $mail->Username = $this->empresa->email_config['mail_user']; } $mail->Password = $this->empresa->email_config['mail_password']; $mail->From = $this->empresa->email; $mail->FromName = $this->user->get_agente_fullname(); $mail->addReplyTo($_POST['de'], $mail->FromName); $mail->addAddress($_POST['email'], $this->cliente->razonsocial); if ($_POST['email_copia']) { if (isset($_POST['cco'])) { $mail->addBCC($_POST['email_copia'], $this->cliente->razonsocial); } else { $mail->addCC($_POST['email_copia'], $this->cliente->razonsocial); } } if ($this->empresa->email_config['mail_bcc']) { $mail->addBCC($this->empresa->email_config['mail_bcc']); } if ($doc == 'factura') { $mail->Subject = $this->empresa->nombre . ': Su factura ' . $this->factura->codigo; } else { $mail->Subject = $this->empresa->nombre . ': Su ' . FS_ALBARAN . ' ' . $this->albaran->codigo; } $mail->AltBody = $_POST['mensaje']; $mail->msgHTML(nl2br($_POST['mensaje'])); $mail->isHTML(TRUE); $mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $filename); if (is_uploaded_file($_FILES['adjunto']['tmp_name'])) { $mail->addAttachment($_FILES['adjunto']['tmp_name'], $_FILES['adjunto']['name']); } $SMTPOptions = array(); if ($this->empresa->email_config['mail_low_security']) { $SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)); } if ($mail->smtpConnect($SMTPOptions)) { if ($mail->send()) { $this->new_message('Mensaje enviado correctamente.'); /// nos guardamos la fecha de envío if ($doc == 'factura') { $this->factura->femail = $this->today(); $this->factura->save(); } else { $this->albaran->femail = $this->today(); $this->albaran->save(); } } else { $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); } } else { $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); } unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $filename); } else { $this->new_error_msg('Imposible generar el PDF.'); } } }
/** * Get smtp connection. * * @param type $request * * @return int */ public function getSmtp($request) { // dd($request); $sending_status = $request->input('sending_status'); // cheking for the sending protocol if ($request->input('sending_protocol') == 'smtp') { $mail = new \PHPMailer(); $mail->isSMTP(); $mail->Host = $request->input('sending_host'); // Specify main and backup SMTP servers //$mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = $request->input('email_address'); // SMTP username $mail->Password = $request->input('password'); // SMTP password $mail->SMTPSecure = $request->input('sending_encryption'); // Enable TLS encryption, `ssl` also accepted $mail->Port = $request->input('sending_port'); // TCP port to connect to if (!$request->input('smtp_validate')) { $mail->SMTPAuth = true; // Enable SMTP authentication $mail->SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)); if ($mail->smtpConnect($mail->SMTPOptions) == true) { $mail->smtpClose(); $return = 1; } else { $return = 0; } } else { if ($mail->smtpConnect() == true) { $mail->smtpClose(); $return = 1; } else { $return = 0; } } } elseif ($request->input('sending_protocol') == 'mail') { $return = 1; } return $return; }